Java 当您的路径为`/storage/1018-2710/Pictures/oLvCVPZrNxk.jpg时,请在Android中删除文件`
所以我试图从Android外部存储中删除一个文件,问题是,我已经尝试了多次以多种不同的方式获取路径。使用MediaStoreAPI,我已经非常接近了,但我不知道如何做到这一点。 我无法获取Java 当您的路径为`/storage/1018-2710/Pictures/oLvCVPZrNxk.jpg时,请在Android中删除文件`,java,android,android-fragments,mediastore,android-storage,Java,Android,Android Fragments,Mediastore,Android Storage,所以我试图从Android外部存储中删除一个文件,问题是,我已经尝试了多次以多种不同的方式获取路径。使用MediaStoreAPI,我已经非常接近了,但我不知道如何做到这一点。 我无法获取EXTERNAL_URI,因为投影需要字符串,而MediaStore上的EXTERNAL_URI返回的URI不是字符串 这就是我加载图像的方式,GalleryImage类只包含文件路径/storage/1018-2710/Pictures/oLvCVPZrNxk.jpg,以及一些用于处理选择的布尔值。我尝试添加
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;
}
}
我尝试了4,5种不同的组合,但似乎都不起作用,我正在尝试获得图像的正确路径,我看到使用外部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();
}
希望它能帮助某人您添加运行时权限了吗?您使用的是什么api?请告诉我们所使用的设备的编译和目标版本以及Android版本。您是如何获得您要谈论的路径的?
String externalURI=“”//空的,因为我无法让它工作…
我们让它工作,因为这也是您应该用来从媒体存储中删除文件的uri。file file file=new file(root+I.getmImageUrl())代码>请告诉一些图像的file.getAbsolutePath()
的值。
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();
}