java Android无法在sd卡上写入和删除操作
我现在正在开发ImageCompressor应用程序
我需要delete
和write
(更新)映像文件。在内部存储中工作得很好,但**SD卡不能给我删除和写入文件的权限
我的应用程序如何能够在sd卡上执行write
和delete
操作(可移动存储)
我已经完成了整个项目,所以我必须想办法
更新:
我已经在做研究了;讨论这个问题。我知道我必须使用storage access framework
,但我对SAF是新手
我使用了库来压缩需要文件而不是Uri的照片。为此,我使用Uri -> File
并使用Intent.ACTION_OPEN_DOCUMENT
从可移动存储中选择图像
But for removable storage I can't find Image Real Path from uri.
我不知道这是不是正确的方式。如果在SAF中有任何方法可以使用uri压缩我的图像,请告诉我。或者如何从可移动存储照片的uri获取图像真实路径
更新代码SAF:
// ----------- Intent -------------
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
// ------------ On Activity Result --------------
Uri uri = data.getData();
try {
ParcelFileDescriptor fileDescriptor = getContentResolver().openFileDescriptor(uri, "w");
FileOutputStream fos = new FileOutputStream(fileDescriptor.getFileDescriptor());
FileInputStream fis = new FileInputStream(getImageFilePath(uri));
FileChannel source = fis.getChannel();
FileChannel destination = fos.getChannel();
destination.transferFrom(source, 0, source.size());
fis.close();
fos.close();
fileDescriptor.close();
Toast.makeText(this, "File save successfully.", Toast.LENGTH_SHORT).show();
}
Uri到文件路径,我从媒体应用程序(如Gallary、Photos)中选择图像,但从sd卡中选择什么将代替MediaStore.Images.Media.DATA
我不知道。代码:
private File getImageFilePath(Uri uri) throws IOException {
String image_id = null, imagePath = null;
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
image_id = cursor.getString(0);
image_id = image_id.substring(image_id.lastIndexOf(":") + 1);
cursor.close();
}
cursor = getContentResolver().query(安卓.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, MediaStore.Images.Media._ID + " = ? ", new String[]{image_id}, null);
if (cursor!=null) {
cursor.moveToFirst();
imagePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
}
File file = new File(imagePath);
return new Compressor(this).setQuality(50).compressToFile(file);
}
# 1 楼答案
如果使用File和FileOutputStream类,可移动SD卡只能在现代Android设备上写入
如果幸运的话,使用
getExternalFilesDirs()
的设备会在卡片上返回一个特定于应用程序的目录,作为第二项,您仍然可以在其中写入内容对于其余部分,使用Storage Access Framework
首先让用户选择带有
Intent.ACTION_OPEN_DOCUMENT_TREE
的sd卡或带有Intent.ACTION_OPEN_DOCUMENT
的文件# 2 楼答案
我挖掘了我的一个旧安卓应用程序,并检索到:
此方法将sdcard uri的根转换为
File
路径为了检索sdcard根的uri,我使用了:
然后用户会选择SD卡的根,然后,我像这样处理结果:
我希望这就是你想要的。有了它,你可以读取/写入/删除SD卡上你想要的任何文件
# 3 楼答案
你必须试试