[英]How To Delete A File Using MediaStore?
I just recently realized that Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
has been depreciated in Android Q .我最近才意识到Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
已在Android Q 中折旧。 I've done some research that the following code works when downloading pictures from the internet.我做了一些研究,发现以下代码在从 Internet 下载图片时有效。
String fileName = String.format(imageName, System.currentTimeMillis());
File dir = new File(Environment.DIRECTORY_PICTURES + "/MyFolder").getAbsoluteFile();
FileOutputStream outStream = null;
ContentResolver cr = getContentResolver();
ContentValues cv = new ContentValues();
cv.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName);
cv.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
cv.put(MediaStore.MediaColumns.RELATIVE_PATH, (Environment.DIRECTORY_PICTURES + "/MyFolder"));
Uri uri = cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cv);
assert uri != null;
scanFile(context, Uri.fromFile(dir));
try {
outStream = (FileOutputStream) cr.openOutputStream(uri);
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext, "Picture Download Failed!", Toast.LENGTH_SHORT).show();
}
dir.mkdirs();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
However, I can't seem to figure out how to delete this picture file that the code above created in "MyFolder"但是,我似乎无法弄清楚如何删除上面代码在“MyFolder”中创建的这个图片文件
I tried the following codes but to no avail...我尝试了以下代码但无济于事...
cr.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null);
This works, but it deletes all picture files in "MyFolder" at once that was downloaded with the code above.这有效,但它会立即删除使用上述代码下载的“MyFolder”中的所有图片文件。 I want it to delete the specified picture file corresponding with the fileName only.我希望它只删除与fileName对应的指定图片文件。 It should only delete one picture at-a-time.它一次只能删除一张图片。
File picFile = new File("/storage/emulated/0/Pictures/MyFolder/" + imageName + ".png");
Uri deleteUri = Uri.parse(picFile.toString());
cr.delete(deleteUri,null, null);
This doesn't work at all.这根本行不通。 It just crashes the app.它只会使应用程序崩溃。 Any solutions?任何解决方案? Thanks!谢谢!
Here is the crash code...这是崩溃代码...
2020-03-25 05:10:10.763 17371-17371/com.exampleproject.myapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.exampleproject.myapp , PID: 17371
java.lang.IllegalArgumentException: Unknown URL /storage/emulated/0/Pictures/MyFolder/The Late Late Show with James Corden.png
at android.content.ContentResolver.delete(ContentResolver.java:1952)
at com.exampleproject.myapp .MyAppPlayer$15.onClick(MyAppPlayer.java:1047)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:191)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7770)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047)
I just tried the following code.我刚刚尝试了以下代码。 It doesn't crash the app, but it doesn't delete anything.它不会使应用程序崩溃,但不会删除任何内容。
Uri uri = cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cv);
assert uri != null;
cr.delete(uri, null, null);
I feel like I am getting closer, but it's still not working.我觉得我越来越近了,但它仍然不起作用。 Here is what I changed it to from your example code... It doesn't crash the app, but the file still doesn't delete.这是我从您的示例代码中将其更改为的内容...它不会使应用程序崩溃,但文件仍然不会删除。
String fileName = String.format(imageName, System.currentTimeMillis());
File dir = new File(Environment.DIRECTORY_PICTURES + "/MyFolder").getAbsoluteFile();
FileOutputStream outStream = null;
ContentResolver cr = getContentResolver();
ContentValues cv = new ContentValues();
String[] selArgs = new String[] {(myFile.getName())};
cv.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName);
cv.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
cv.put(MediaStore.MediaColumns.RELATIVE_PATH, (Environment.DIRECTORY_PICTURES + "/MyFolder"));
cr.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Images.Media._ID + "=?", selArgs);
If you want to delete specified picture :如果要删除指定图片:
Use用
Uri uri = cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cv);
instead of代替
Uri deleteUri = Uri.parse(new File("/storage/emulated/0/Pictures/MyFolder/" + imageName + ".png"));
Because your file uri is something like this:因为你的文件 uri 是这样的:
content://media/external/images/media/45
There is no delete uri, so you get this error "Unknown URL"没有删除 uri,因此您会收到此错误“未知 URL”
Delete func:删除函数:
public Boolean deleteFile(Context context, Uri uri) {
File file = new File(uri.getPath());
String [] selectionArgs = {(file.getAbsolutePath())};
ContentResolver contentResolver = context.getContentResolver();
String where = null;
Uri filesUri = null;
if (android.os.Build.VERSION.SDK_INT >= 29) {
filesUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
where = MediaStore.Images.Media._ID + "=?";
selectionArgs = new String[] {(file.getName())};
} else {
where = MediaStore.MediaColumns.DATA + "=?";
filesUri = MediaStore.Files.getContentUri("external");
}
int result = contentResolver.delete(filesUri, where, selectionArgs);
return file.exists();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.