android6.0下载文件路径设置
最近有很多朋友问我,android6.0下载文件怎样设置文件的保存路径。今天就把android6.0下载文件路径设置总结一下。
在android 6.0以前,你可以只关注外置存储是否挂载即可,并且可以直接在sdcard目录下创建“xxx”文件夹保存下载的文件(例如下载一张图片a.jpg到sdcar“xxx”文件夹下,我们只需要在清单文件中配置对sdcard读写权限,就可以设置下载路径“mnt/sdcard/xxx/a.jpg”)。但是从6.0以后,也就是M系统后,还需要判断是否有读写权限,只有具备这些权限才可以读写外置存储。并且要通过这个方法:Environment.getExternalStorageDirectory()获取路径:/storage/emulated/0
默认存在,声明权限则可读写(6.0和以后系统还需要向用户申请同意才可以)。
下面直接上代码:
Java代码:
package com.hc.android6.download;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
import org.hpg.weather.download.R;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
public class MainActivity extends ActionBarActivity {
private final int TODOWN = 1;
private static final String TAG = "hc";
private File file1;
private int contentLength;
private Handler mhandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case TODOWN:
// 设置下载进度条的进度
if (file1.length() == contentLength) {
Toast.makeText(getApplicationContext(), "下载完成", Toast.LENGTH_SHORT).show();
// 隐藏进度条
mprogressBar.setVisibility(View.GONE);
} else {
setmprogressbar();
}
break;
}
}
};
private void setmprogressbar() {
mprogressBar.setProgress((int) file1.length());
}
String hcUrl = "http://p1.so.qhmsg.com/t016269623a51ead026.jpg";
private Button button;
private ProgressBar mprogressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.download);
mprogressBar = (ProgressBar) findViewById(R.id.progressBar);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
download();
}
}).start();
}
});
}
//下载
private void download() {
Message message = new Message();
message.what = TODOWN;
try {
URL url = new URL(hcUrl);
//打开连接
URLConnection conn = url.openConnection();
//打开输入流
InputStream is = conn.getInputStream();
//获得长度
contentLength = conn.getContentLength();
mprogressBar.setMax(contentLength);
Log.e(TAG, "contentLength = " + contentLength);
//创建文件夹 HC,在存储卡下
String dirName = Environment.getExternalStorageDirectory() + "/HC/";
File file = new File(dirName);
//不存在创建
if (!file.exists()) {
file.mkdir();
}
//下载后的文件名
String fileName = dirName + "hcdown.jpg";
file1 = new File(fileName);
if (file1.exists()) {
file1.delete();
}
//创建字节流
byte[] bs = new byte[1024];
int len;
OutputStream os = new FileOutputStream(fileName);
//写数据
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
mhandler.sendEmptyMessage(TODOWN);
SystemClock.sleep(100);
}
//完成后关闭流
Log.e(TAG, "download-finish");
Log.e(TAG, file1.length() + "");
os.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
xml布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.hc.android6.download.MainActivity">
<Button
android:layout_marginTop="10dp"
android:id="@+id/download"
android:text="开始下载图片"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ProgressBar
android:layout_below="@+id/download"
android:layout_marginTop="10dp"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="5dp"
android:id="@+id/progressBar" />
</RelativeLayout>