![](/img/trans.png)
[英]Android Java, FileProviders, getUriFromFile failed with error Failed to find configured root that contains /storage/1018-2710/Pictures/Sarx7IIJi-o.jpg
[英]Delete a file in Android when you have a path of form `/storage/1018-2710/Pictures/oLvCVPZrNxk.jpg`
所以我試圖從 Android 外部存儲中刪除一個文件,問題是,我嘗試以多種不同的方式多次獲取路徑。 使用 MediaStore Api 我非常接近,但不知道如何去做。 我無法獲得EXTERNAL_URI
因為投影需要字符串,而 MediaStore 上的EXTERNAL_URI
返回一個 URI 而不是字符串。
這就是我加載圖像的方式,GalleryImage 類只包含文件路徑/storage/1018-2710/Pictures/oLvCVPZrNxk.jpg
和一些布爾值來處理選擇。 我嘗試添加外部 URI 但未能這樣做。
public class ImageLoader {
public ArrayList<GalleryImage> getAllShownImagesPath(Context context) {
Uri uri;
Cursor cursor;
int column_index_data, column_index_folder_name, column_index_content_uri;
ArrayList<GalleryImage> listOfAllImages = new ArrayList<GalleryImage>();
String absolutePathOfImage = null;
uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaStore.MediaColumns.DATA,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
};
cursor = context.getContentResolver().query(uri, projection, null,
null, null);
column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
column_index_folder_name = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
while (cursor.moveToNext()) {
absolutePathOfImage = cursor.getString(column_index_data);
// String externalURI = cursor.getString();
String externalURI = ""; //empty, beacuse I couldnt get it to work...
listOfAllImages.add(new GalleryImage(false, false , absolutePathOfImage, externalURI));
}
cursor.close();
return listOfAllImages;
}
}
在我的片段中,我試圖刪除圖像
if(item.getItemId() == R.id.delete_selected){
//Uri root = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String root = Environment.getExternalStorageDirectory().toString();
for(GalleryImage i : mViewModel.selectedImages){
File file = new File(root + i.getmImageUrl());
if (file.exists()) {
if (file.delete()) {
System.out.println("file Deleted :" + file.getPath());
} else {
System.out.println("file not Deleted :" + file.getPath());
}
}
}
}
我嘗試了 4,5 種不同的組合,但似乎都不起作用,我正在嘗試獲取圖像的正確路徑,我看到使用EXTERNAL_URI
可能是可行的方法,但我不知道如何獲取它進入我的 GalleryImage。 任何幫助表示贊賞!
public void deleteSelectedImages(){
for(GalleryImage img : selectedImages){
File file = new File(img.getmImageUrl());
if(!file.exists()) {
return;
}
String canonicalPath;
ContentResolver contentResolver = getApplication().getContentResolver();
try {
canonicalPath = file.getCanonicalPath();
} catch (IOException e) {
canonicalPath = file.getAbsolutePath();
}
final Uri uri = MediaStore.Files.getContentUri("external");
final int result = contentResolver.delete(uri,
MediaStore.Files.FileColumns.DATA + "=?", new String[]{canonicalPath});
if (result == 0) {
final String absolutePath = file.getAbsolutePath();
if (!absolutePath.equals(canonicalPath)) {
contentResolver.delete(uri,
MediaStore.Files.FileColumns.DATA + "=?", new String[]{absolutePath});
}
}
images.remove(img);
}
imagesLiveData.setValue(images);
selectedImages.clear();
Toast.makeText(getApplication().getApplicationContext(), "Succesfully deleted image(s)", Toast.LENGTH_SHORT).show();
}
希望它會幫助某人
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.