文章參考
AndroidQ 適配-存儲空間篇
Android Q 要來了,給你一份很"全面"的適配指南!
適配Android Q拍照和讀取相冊圖片
Android-Q適配-存儲方式
Android Q 沙箱適配多媒體文件總結(jié)
AndroidQ 適配-存儲空間篇
Android Q 沙箱適配多媒體文件總結(jié)
Android Q 存儲機(jī)制大變化
Android Q(10) 文件存儲適配
博客內(nèi)容新增
2019-12-20
1搁廓、將公共目錄文件保存在沙盒目錄下面
2020-03-17
1、修改下載文件到公共目錄文件夾下耕皮,不僅僅可以下載Music境蜕、Movies、Pictures凌停、還可以下載普通的excel文件等
問題描述
最近升級到anroid10粱年,google對手機(jī)根目錄文件夾的創(chuàng)建越來越嚴(yán)格了,我倒是覺得這個是一件好事情苦锨,但是逼泣!適配是個頭疼的問題趴泌。我目前處理 android<10 和 >=10 的情況,下載進(jìn)度監(jiān)聽拉庶。
代碼太多嗜憔,只展示部分,剩下代碼請到 github
代碼展示
/**
* @date: 創(chuàng)建時間:2019/12/11
* @author: gaoxiaoxiong
* @descripion:文件操作類的監(jiān)聽
**/
public interface OnFileDownListener {
/**
* @date :2019/12/16 0016
* @author : gaoxiaoxiong
* @description:
* @param status status == -1 表示失敗 status ==0 表示正在下載 status == 1 表示成功
* @param object 返回成功后的對象參數(shù)
* @param proGress 當(dāng)前下載百分比
* @param currentDownProGress 當(dāng)前下載量
* @param totalProGress 總的量大小
**/
void onFileDownStatus(int status,Object object,int proGress, long currentDownProGress, long totalProGress);
}
/**
* @date: 2019/5/22 0022
* @author: gaoxiaoxiong
* @description: 下載的圖片保存的位置
**/
public String getPublickDiskImagePicDir() {
return getPublickDiskFileDir(BaseApplication.getInstance(), DIRECTORY_PICTURES);
}
/**
* @date: 2019/5/22 0022
* @author: gaoxiaoxiong
* @description: 電影保存的位置
**/
public String getPublickDiskMoviesDir() {
return getPublickDiskFileDir(BaseApplication.getInstance(), DIRECTORY_MOVIES);
}
/**
* @date :2019/12/16 0016
* @author : gaoxiaoxiong
* @description:音樂保存的位置
**/
public String getPublickDiskMusicDir() {
return getPublickDiskFileDir(BaseApplication.getInstance(), DIRECTORY_MUSIC);
}
/**
* @date 創(chuàng)建時間:2018/12/20
* @author GaoXiaoXiong
* @Description: 創(chuàng)建保存圖片的緩存目錄
*/
public String getPublickDiskImagePicCacheDir() {
return getPublickDiskCacheDir(BaseApplication.getInstance(), DIRECTORY_PICTURES);
}
/**
* 作者:GaoXiaoXiong
* 創(chuàng)建時間:2019/1/26
* 注釋描述:獲取緩存目錄
*
* @fileName 獲取外部存儲目錄下緩存的 fileName的文件夾路徑
*/
public String getPublickDiskCacheDir(Context context, String fileName) {
String cachePath = null;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
|| !Environment.isExternalStorageRemovable()) {//此目錄下的是外部存儲下的私有的fileName目錄
cachePath = context.getExternalCacheDir().getPath() + "/" + fileName; //SDCard/Android/data/你的應(yīng)用包名/cache/fileName
} else {
cachePath = context.getCacheDir().getPath() + "/" + fileName;
}
File file = new File(cachePath);
if (!file.exists()) {
file.mkdirs();
}
return file.getAbsolutePath(); //SDCard/Android/data/你的應(yīng)用包名/cache/fileName
}
/**
* @date: 2019/8/2 0002
* @author: gaoxiaoxiong
* @description:獲取外部存儲目錄下的 fileName的文件夾路徑
**/
public String getPublickDiskFileDir(Context context, String fileName) {
String cachePath = null;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
|| !Environment.isExternalStorageRemovable()) {//此目錄下的是外部存儲下的私有的fileName目錄
cachePath = context.getExternalFilesDir(fileName).getAbsolutePath(); //mnt/sdcard/Android/data/com.my.app/files/fileName
} else {
cachePath = context.getFilesDir().getPath() + "/" + fileName; //data/data/com.my.app/files
}
File file = new File(cachePath);
if (!file.exists()) {
file.mkdirs();
}
return file.getAbsolutePath(); //mnt/sdcard/Android/data/com.my.app/files/fileName
}
/**
* @date :2020/3/17 0017
* @author : gaoxiaoxiong
* @description:獲取公共目錄氏仗,注意吉捶,只適合android9.0以下的
**/
public String getPublickDiskFileDirAndroid9(String fileDir){
String filePath = null;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
|| !Environment.isExternalStorageRemovable()) {
filePath = Environment.getExternalStoragePublicDirectory(fileDir).getPath();
}
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
return file.getAbsolutePath();
}
public static final int LOADING = 0;//加載中
public static final int SUCCESS=1;
public static final int FAIL=-1;
/**
* 如果是要存放到沙盒外部目錄,就需要使用此方法
* @date: 創(chuàng)建時間:2019/12/11
* @author: gaoxiaoxiong
* @descripion: 保存圖片皆尔,視頻呐舔,音樂到公共地區(qū),此操作需要在線程慷蠕,不是我們自己的APP目錄下面的
* @param downPathUrl 下載文件的路徑珊拼,需要包含后綴
* @param inserType 存儲類型,可選參數(shù) DIRECTORY_PICTURES ,DIRECTORY_MOVIES ,DIRECTORY_MUSIC 流炕,DIRECTORY_DOWNLOADS
**/
public void downFileFromServiceToPublicDir(String downPathUrl, Context context, String inserType, OnFileDownListener onFileDownListener) {
if (inserType.equals(DIRECTORY_DOWNLOADS)){
if (Build.VERSION.SDK_INT>=29){//android 10
downUnKnowFileFromService(downPathUrl,context,inserType,onFileDownListener);//返回的是uri
}else {
downUnKnowFileFromService(downPathUrl,onFileDownListener);//返回的是file
}
}else {
//下載到沙盒外部公共目錄
downMusicVideoPicFromService(downPathUrl,context,inserType,onFileDownListener);
}
}
/**
* 如果是要存放到沙盒外部目錄澎现,就需要使用此方法
* @date: 創(chuàng)建時間:2019/12/11
* @author: gaoxiaoxiong
* @descripion: 下載的文件到 DIRECTORY_DOWNLOADS,只有10以上才有 MediaStore.Downloads
* @param downPathUrl 下載文件的路徑每辟,需要包含后綴
* @param inserType 存儲類型 DIRECTORY_DOWNLOADS
**/
private void downUnKnowFileFromService(final String downPathUrl,final Context context, String inserType,final OnFileDownListener onFileDownListener){
if (inserType.equals(DIRECTORY_DOWNLOADS)){
Observable.just(downPathUrl).subscribeOn(Schedulers.newThread()).map(new Function<String, Uri>() {
@RequiresApi(api = Build.VERSION_CODES.Q)
@Override
public Uri apply(String s) throws Exception {
Uri uri = null;
try {
URL url = new URL(downPathUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(30 * 1000);
InputStream is = conn.getInputStream();
long time = System.currentTimeMillis();
int code = conn.getResponseCode();
String prefix = downPathUrl.substring(downPathUrl.lastIndexOf(".") + 1);
String fileName = null;
if (code == HttpURLConnection.HTTP_OK) {
fileName = conn.getHeaderField("Content-Disposition");
// 通過Content-Disposition獲取文件名剑辫,這點(diǎn)跟服務(wù)器有關(guān),需要靈活變通
if (fileName == null || fileName.length() < 1) {
// 通過截取URL來獲取文件名
URL downloadUrl = conn.getURL(); // 獲得實(shí)際下載文件的URL
fileName = downloadUrl.getFile();
fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
} else {
fileName = URLDecoder.decode(fileName.substring(
fileName.indexOf("filename=") + 9), "UTF-8");
// 有些文件名會被包含在""里面渠欺,所以要去掉妹蔽,不然無法讀取文件后綴
fileName = fileName.replaceAll("\"", "");
}
}
if (isEmpty(fileName)) {
fileName = time + "." + prefix;
}
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Downloads.DISPLAY_NAME, fileName);
contentValues.put(MediaStore.Downloads.MIME_TYPE,getMIMEType(fileName));
contentValues.put(MediaStore.Downloads.DATE_TAKEN, System.currentTimeMillis());
uri = context.getContentResolver().insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues);
BufferedInputStream inputStream = new BufferedInputStream(is);
OutputStream os = context.getContentResolver().openOutputStream(uri);
if (os != null) {
byte[] buffer = new byte[1024];
int len;
int total = 0;
int contentLeng = conn.getContentLength();
while ((len = inputStream.read(buffer)) != -1) {
os.write(buffer, 0, len);
total += len;
if (onFileDownListener != null) {
onFileDownListener.onFileDownStatus(LOADING, null, (total * 100 / contentLeng), total, contentLeng);
}
}
}
os.flush();
inputStream.close();
is.close();
os.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return uri;
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Uri>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Uri uri) {
if (uri != null && onFileDownListener != null) {
onFileDownListener.onFileDownStatus(SUCCESS, uri, 0, 0, 0);
} else {
onFileDownListener.onFileDownStatus(FAIL, null, 0, 0, 0);
}
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
}
/**
* 如果是要存放到沙盒外部目錄,就需要使用此方法
* @date: 創(chuàng)建時間:2019/12/11
* @author: gaoxiaoxiong
* @descripion: 保存圖片挠将,視頻胳岂,音樂到公共地區(qū),此操作需要在線程捐名,不是我們自己的APP目錄下面的
* @param downPathUrl 下載文件的路徑旦万,需要包含后綴
* @param inserType 存儲類型闹击,可選參數(shù) DIRECTORY_PICTURES ,DIRECTORY_MOVIES ,DIRECTORY_MUSIC
**/
private void downMusicVideoPicFromService(final String downPathUrl,final Context context,final String inserType,final OnFileDownListener onFileDownListener){
Observable.just(downPathUrl).subscribeOn(Schedulers.newThread()).map(new Function<String, Uri>() {
@Override
public Uri apply(String s) throws Exception {
Uri uri = null;
try {
URL url = new URL(downPathUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(30 * 1000);
InputStream is = conn.getInputStream();
long time = System.currentTimeMillis();
int code = conn.getResponseCode();
String prefix = downPathUrl.substring(downPathUrl.lastIndexOf(".") + 1);
String fileName = null;
if (code == HttpURLConnection.HTTP_OK) {
fileName = conn.getHeaderField("Content-Disposition");
// 通過Content-Disposition獲取文件名镶蹋,這點(diǎn)跟服務(wù)器有關(guān),需要靈活變通
if (fileName == null || fileName.length() < 1) {
// 通過截取URL來獲取文件名
URL downloadUrl = conn.getURL(); // 獲得實(shí)際下載文件的URL
fileName = downloadUrl.getFile();
fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
} else {
fileName = URLDecoder.decode(fileName.substring(
fileName.indexOf("filename=") + 9), "UTF-8");
// 有些文件名會被包含在""里面赏半,所以要去掉贺归,不然無法讀取文件后綴
fileName = fileName.replaceAll("\"", "");
}
}
if (isEmpty(fileName)) {
fileName = time + "." + prefix;
}
ContentValues contentValues = new ContentValues();
if (inserType.equals(DIRECTORY_PICTURES)) {
contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);
contentValues.put(MediaStore.Images.Media.MIME_TYPE, getMIMEType(fileName));
contentValues.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
//只是往 MediaStore 里面插入一條新的記錄,MediaStore 會返回給我們一個空的 Content Uri
//接下來問題就轉(zhuǎn)化為往這個 Content Uri 里面寫入
uri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
} else if (inserType.equals(DIRECTORY_MOVIES)) {
contentValues.put(MediaStore.Video.Media.MIME_TYPE, getMIMEType(fileName));
contentValues.put(MediaStore.Video.Media.DISPLAY_NAME, fileName);
contentValues.put(MediaStore.Video.Media.DATE_TAKEN, System.currentTimeMillis());
//只是往 MediaStore 里面插入一條新的記錄断箫,MediaStore 會返回給我們一個空的 Content Uri
//接下來問題就轉(zhuǎn)化為往這個 Content Uri 里面寫入
uri = context.getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, contentValues);
} else if (inserType.equals(DIRECTORY_MUSIC)) {
contentValues.put(MediaStore.Audio.Media.MIME_TYPE, getMIMEType(fileName));
contentValues.put(MediaStore.Audio.Media.DISPLAY_NAME, fileName);
if (Build.VERSION.SDK_INT>=29){//android 10
contentValues.put(MediaStore.Audio.Media.DATE_TAKEN, System.currentTimeMillis());
}
//只是往 MediaStore 里面插入一條新的記錄拂酣,MediaStore 會返回給我們一個空的 Content Uri
//接下來問題就轉(zhuǎn)化為往這個 Content Uri 里面寫入
uri = context.getContentResolver().insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, contentValues);
}
BufferedInputStream inputStream = new BufferedInputStream(is);
OutputStream os = context.getContentResolver().openOutputStream(uri);
if (os != null) {
byte[] buffer = new byte[1024];
int len;
int total = 0;
int contentLeng = conn.getContentLength();
while ((len = inputStream.read(buffer)) != -1) {
os.write(buffer, 0, len);
total += len;
if (onFileDownListener != null) {
onFileDownListener.onFileDownStatus(LOADING, null, (total * 100 / contentLeng), total, contentLeng);
}
}
}
//oppo手機(jī)不會出現(xiàn)在照片里面,但是會出現(xiàn)在圖集里面
if (inserType.equals(DIRECTORY_PICTURES)){//如果是圖片
//掃描到相冊
String[] filePathArray = FileSDCardUtil.getInstance().getPathFromContentUri(uri,context);
MediaScannerConnection.scanFile(context, new String[] {filePathArray[0]}, new String[]{"image/jpeg"}, new MediaScannerConnection.OnScanCompletedListener(){
@Override
public void onScanCompleted(String path, Uri uri) {
Log.e(TAG,"PATH:"+path);
}
} );
}
os.flush();
inputStream.close();
is.close();
os.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return uri;
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Uri>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Uri uri) {
if (uri != null && onFileDownListener != null) {
onFileDownListener.onFileDownStatus(SUCCESS, uri, 0, 0, 0);
} else {
onFileDownListener.onFileDownStatus(FAIL, null, 0, 0, 0);
}
}
@Override
public void onError(Throwable e) {
Log.e(TAG,"錯誤信息:"+e.getMessage());
}
@Override
public void onComplete() {
}
});
}