当路径存储在SQLite DB中时,如何从Android Internal中删除图像文件
我知道这个问题已经得到了回答。但在使用SQLite DB时,我无法理解它。我的应用程序捕获一些文档并将存储在手机内存中。我正在我的应用程序中使用SQLite DB,它存储上述图像的路径。如果在SQLite DB中删除图像,如何从手机内存中删除图像当路径存储在SQLite DB中时,如何从Android Internal中删除图像文件,android,android-file,Android,Android File,我知道这个问题已经得到了回答。但在使用SQLite DB时,我无法理解它。我的应用程序捕获一些文档并将存储在手机内存中。我正在我的应用程序中使用SQLite DB,它存储上述图像的路径。如果在SQLite DB中删除图像,如何从手机内存中删除图像 String photoPath=cursor.getString(i_COL_PICTURE) --我的路径是 `"content://com.google.android.apps.photos.contentprovider/-1/1/conte
String photoPath=cursor.getString(i_COL_PICTURE)代码>
--我的路径是
`"content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F153/ORIGINAL/NONE/1743496576"
`当您想删除存储中的某些文件时,只需执行此操作
File file = new File(yourFilePathHere);
deleted = file.delete();
我认为您具有所需的权限,因为您可以在存储器中写入文件
编辑
您正在使用MediaStore
获取图像。所以现在,当您想要删除文件时,您也应该从MediaStore
中删除文件。我有一个方法可以帮助你
public static int deleteFileFromMediaStore(final ContentResolver contentResolver, final File file) {
String canonicalPath;
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)) {
int deletedRow = contentResolver.delete(uri,
MediaStore.Files.FileColumns.DATA + "=?", new String[]{absolutePath});
return deletedRow;
}
} else return result;
return result;
}
在你的活动中调用它
deleteFileFromMediaStore(getContentResolver(), fileToDelete)
注意检查您是否通过MediaStore
获取绝对路径。如果你的代码有问题,下面是我获取所有画廊图片的方法
public static ArrayList<ModelBucket> getImageBuckets(Context context) {
ArrayList<ModelBucket> list = new ArrayList<>();
String absolutePathOfImage;
String absoluteFolder;
boolean same_folder = false;
int pos = 0;
Uri uri;
Cursor cursor;
int column_index_data, column_index_folder_name;
uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.MediaColumns.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME};
final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
cursor = context.getContentResolver().query(uri, projection, null, null, orderBy + " DESC");
if (cursor == null) return 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);
absoluteFolder = cursor.getString(column_index_folder_name);
Log.d("Column", absolutePathOfImage);
Log.d("Folder", absoluteFolder);
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getFolderName().equals(absoluteFolder)) {
same_folder = true;
pos = i;
break;
} else {
same_folder = false;
}
}
if (same_folder) {
ArrayList<String> al_path = new ArrayList<>(list.get(pos).getAllFilesPath());
al_path.add(absolutePathOfImage);
list.get(pos).setAllFilesPath(al_path);
} else {
ArrayList<String> al_path = new ArrayList<>();
al_path.add(absolutePathOfImage);
ModelBucket modelBucket = new ModelBucket();
modelBucket.setFolderName(absoluteFolder);
modelBucket.setAllFilesPath(al_path);
list.add(modelBucket);
}
}
return list;
}
在删除图像之前,获取图像的路径并将路径传递给下面的代码
File fdelete = new File(path);
if (fdelete.exists()) {
if (fdelete.delete()) {
System.out.println("file Deleted :" + path);
} else {
System.out.println("file not Deleted :" + path);
}
}
在此之后,如果您的Uri指向可以执行的文件,请从sqlite db中删除路径:
String pathToFile = myUri.getEncodedPath(); // this gives your the real path to the file, like /emulated/0/sdcard/myImageFile.jpg
File file = new File(pathToFile);
if(file.exists()){
file.delete();
}
只需删除文件。@VladyslavMatviienko我的应用程序要求也删除存储在路径中的图像。图像存储在文件中。删除文件,即itUri myUri=Uri.parse(photoPath),然后删除字符串pathToFile=myUri.getEncodedPath();其余的你都知道。看看我的答案:)这是删除文件的正确方法,现在通过调试检查您的路径,检查您路径上的文件是否存在。再告诉我你所说的不起作用的意思,你可以告诉我错误。我在问题中更新了我的路径,这个方法对我更新的路径不起作用。当我们访问存储在SQLite DB中的路径时。它给出了上面的路径,我无法提取实际路径。谢谢:)\n您正在从媒体存储中获取文件,需要使用不同的方法来删除。请确保通过cursor.getColumnIndex(MediaStore.MediaColumns.DATA)
获取路径,这将返回文件的绝对路径。顺便说一句,file.delete方法对您没有帮助,因为它从目录而不是媒体存储中删除文件。
String pathToFile = myUri.getEncodedPath(); // this gives your the real path to the file, like /emulated/0/sdcard/myImageFile.jpg
File file = new File(pathToFile);
if(file.exists()){
file.delete();
}