本文是 Android 11 从外部存储读取文件到应用沙盒存储 的兄弟篇 :Android 11 从沙盒拷贝文件到外部共享存储区域,效果:
1. 需求中我们需要把自己应用沙盒的文件拷贝到外部共享存储区域,提供给其他app使用
1)外部共享存储区域:主要是指Enviromnent下的 如
Environment.DIRECTORY_DCIM
Environment.DIRECTORY_DOCUMENTS
Environment.DIRECTORY_DOWNLOADS
Environment.DIRECTORY_MOVIES
Environment.DIRECTORY_PICTURES
Environment.DIRECTORY_MUSIC
等。
2)外部存储区域:指SDCard下任意路径。
在Android11以上已经不能随便访问任意外部存储路径了,并且也不能直接new File形式访问。(其实是在Android 10 谷歌就开始建议,但是没有强制。开发者可以通过在AndroidManifest.xml application节点中加上android:requestLegacyExternalStorage="true"属性还能继续访问外部存储)
2. 拷贝自己应用沙盒路径下文件到外部共享存储区域核心实现:
1)工具方法
/**
* 拷贝沙盒中的文件到外部存储区域
* @param filePath 沙盒文件路径
* @param externalUri 外部存储文件的 uri
*/
public static boolean copySandFileToExternalUri(Context context, String filePath, Uri externalUri) {
ContentResolver contentResolver = context.getContentResolver();
InputStream inputStream = null;
OutputStream outputStream = null;
boolean ret = false;
try {
outputStream = contentResolver.openOutputStream(externalUri);
File sandFile = new File(filePath);
if(sandFile.exists()) {
inputStream = new FileInputStream(sandFile);
int readCount = 0;
byte[] buffer = new byte[1024];
while ((readCount = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0 , readCount);
outputStream.flush();
}
}
ret = true;
} catch (Exception e) {
Log.e(TAG, "copy SandFile To ExternalUri. e = " + e.toString());
ret = false;
} finally {
try {
if(outputStream != null) {
outputStream.close();
}
if(inputStream != null) {
inputStream.close();
}
Log.d(TAG, " input stream and output stream close successful.");
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, " input stream and output stream close fail. e = " + e.toString());
}
return ret;
}
}
2) 调用例子
android 6.0以上需要动态申请权限,因此需要先进行权限判断:
已有权限走else case
/**
* android 6.0以上动态权限申请:写权限
*/
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, FileHandlePresenter.PERMISSION_CODE_WRITE_EXTERNAL);
} else {
fileHandlePresenter.handleWriteExternalStorage();
}
权限申请回调
public void onRequestPermissionsResult(int requestCode, @NonNull @NotNull String[] permissions, @NonNull @NotNull int[] grantResults) {
......
} else if(requestCode == PERMISSION_CODE_WRITE_EXTERNAL) {
if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
handleWriteExternalStorage();
}
}
......
}
获得权限后拷贝文件操作:
/**
* 将沙盒中的文件/data/data/包名/cache/test.txt 保存到外部存储区域 sdcard/test.txt
*/
public void handleWriteExternalStorage() {
File sandFile = new File(mFragment.getActivity().getCacheDir() + File.separator + "test.txt");
if(!sandFile.exists()) {
Toast.makeText(mFragment.getActivity(), "请先手动创建test.txt 并且保存到/data/data/包名/cache/下", Toast.LENGTH_LONG).show();
return;
}
ThreadManager.getInstence().postTask(new Runnable() {
@Override
public void run() {
Uri externalUri = null;
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
File externalFile = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File destFile = new File(externalFile + File.separator + "test.txt");
externalUri = Uri.fromFile(destFile);
} else {
ContentResolver resolver = mFragment.getActivity().getContentResolver();
ContentValues values = new ContentValues();
values.put(MediaStore.Downloads.DISPLAY_NAME, "test.txt");
values.put(MediaStore.Downloads.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS);//保存路径
Uri uri = MediaStore.Files.getContentUri("external");
externalUri = resolver.insert(uri, values);
}
boolean ret = FileHelper.copySandFileToExternalUri(mFragment.getActivity(), sandFile.getAbsolutePath(), externalUri);
mFragment.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(mFragment.getActivity(), "从沙盒" + sandFile.getAbsolutePath() + "中拷贝文件到外部存储"
+ Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()
+ ", 结果= " + ret, Toast.LENGTH_LONG).show();
}
});
}
});
}
Demo地址: