(No such file or directory)
/**
* 第一步:获取文件的总长度,进行文件路径设置
*/
int contentLength = urlConnection.getContentLength();
//获取存取设备的地址
String downloadFolderName = Environment.getExternalStorageDirectory()
+ File.separator+"lemon"+ File.separator;
//通过downloadFolderName获取目录,如果目录不存在就创建目录
File file=new File(downloadFolderName);
if(!file.exists()){
file.mkdirs();
}
//拼接出文件名fileName
String fileName = downloadFolderName+"lemon.apk";
File apkFile = new File(fileName);
if(apkFile.exists()){
apkFile.delete();
}
/**
* 第二步:进行下载
*/
//创建出下载长度
int downloadSize = 0;
//建立字节数组
byte bytes[] = new byte[1024];
int length=0;
//将outputStream下载到文件中
OutputStream outputStream= new FileOutputStream(apkFile);
//设置while循环,每次都读bytes
while ((length = inputStream.read(bytes))!= -1){
outputStream.write(bytes,0,length);
downloadSize +=length;
/**
* Update UI
*/
Message message=Message.obtain();
message.obj=downloadSize * 100/contentLength;
message.what= DOWNLOAD_MESSAGE_CODE;
mHandler.sendMessage(message);
}
inputStream.close();
outputStream.close();
正在回答
1)sdcard0下找你的文件夹:lemon,然后你的文件就在此文件夹下。这个目录就是你sd卡的路径,对于真机来说,不同的商家,映射出来的路径不同,稍有区别。也可以到后面学习参见《Android本地文件操作》。2)另外真机是看不到此目录的,除非你手机被root了,但不建议对真机进行root,因为这样会带来安全隐患。包括一些对安全要求较高的app,一旦root就不可用了。
package com.example.handlerproject;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class DownloadActivity extends Activity {
private static final String TAG = "MainActivity";
public static final int DOWNLOAD_MESSAGE_CODE = 100001;
private static final int DOWNLOAD_MESSAGE_FAIL_CODE = 100002;
public static final String APP_URL = "http://download.sj.qq.com/upload/connAssitantDownload/upload/MobileAssistant_1.apk";
private Handler mHandler;
//添加权限相应字段
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
"android.permission.READ_EXTERNAL_STORAGE",
"android.permission.WRITE_EXTERNAL_STORAGE" };
@Override
protected void onCreate(Bundle savedInstanceState) {
verifyStoragePermissions(DownloadActivity.this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_download);
final ProgressBar progressBar=findViewById(R.id.progressBar);
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread(new Runnable() {
@Override
public void run() {
download(APP_URL);
}
}).start();
}
});
//创建handler用于sendMessage
mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case DOWNLOAD_MESSAGE_CODE:
progressBar.setProgress((Integer) msg.obj);
Log.i(TAG, "handleMessage: Success!");
break;
case DOWNLOAD_MESSAGE_FAIL_CODE:
}
}
};
}
private void download(String appUrl) {
try {
URL url = new URL(appUrl);
URLConnection urlConnection=url.openConnection();
//获得文件流
InputStream inputStream=urlConnection.getInputStream();
/**
* 第一步:获取文件的总长度,进行文件路径设置
*/
int contentLength = urlConnection.getContentLength();
//获取存取设备的地址
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
String downloadFolderName = Environment.getExternalStorageDirectory()
+ File.separator + "lemon";
//通过downloadFolderName获取目录,如果目录不存在就创建目录
File file = new File(downloadFolderName);
if (!file.exists()) {
file.mkdir();
}
//拼接出文件名fileName
String fileName = downloadFolderName + File.separator+ "lemon.apk";
File apkFile = new File(fileName);
if (apkFile.exists()) {
apkFile.delete();
}
/**
* 第二步:进行下载
*/
//创建出下载长度
int downloadSize = 0;
//建立字节数组
byte bytes[] = new byte[1024];
int length = 0;
//将outputStream下载到文件中
OutputStream outputStream = new FileOutputStream(fileName, true);
//设置while循环,每次都读bytes
while ((length = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, length);
downloadSize += length;
/**
* Update UI
*/
Message message = Message.obtain();
message.obj = downloadSize * 100 / contentLength;
message.what = DOWNLOAD_MESSAGE_CODE;
mHandler.sendMessage(message);
}
Log.i(TAG, "handleMessage: DownloadSuccess!");
inputStream.close();
outputStream.close();
}
} catch (MalformedURLException e) {
notifyDownloadFailed();
e.printStackTrace();
} catch (IOException e) {
notifyDownloadFailed();
e.printStackTrace();
}
}
private void notifyDownloadFailed() {
Message message = Message.obtain();
message.what = DOWNLOAD_MESSAGE_FAIL_CODE;
mHandler.sendMessage(message);
}
public static void verifyStoragePermissions(Activity activity){
try {
//检测是否有写的权限
int permission = ActivityCompat.checkSelfPermission(activity,
"android.permission.WRITE_EXTERNAL_STORAGE");
if (permission != PackageManager.PERMISSION_GRANTED) {
// 没有写的权限,去申请写的权限,会弹出对话框
ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 参与学习 人
- 提交作业 307 份
- 解答问题 1613 个
本专题是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理。介绍了Android内外部文件存储、轻量级数据库SQLite的使用。利用屏幕适配、状态保持、百度地图解决实际问题。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星