Android: remove an image from sd card

佐手、 提交于 2019-11-30 11:59:46
Kalpesh

Why would you make it that complex?

You can do it as simple as this:

getContentResolver().delete(Utils.imgUri, null, null);
Matteo

Resolved adding:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));

It makes the MediaScanner service run again, which should remove the deleted image from the device's cache.

same problem here

Have you set the <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> permission in the manifest file?

Stefan H Singer

Are you sure that the file path is correct? Because the way you do the actual delete should be fine, see this SO thread: How to delete a file from SD card?

Should the /mnt/ really be there? Also, do you have the permissions to delete files from the storage? (android.permission.WRITE_EXTERNAL_STORAGE)

Finuka

in the parameter "data" you have the Uri too, just do "data.getUri()". Also, are you testing in a real devices? if so and if it is a samsung, it isn't work (see this thread).

simple one line ;)

 new File(uri.getPath()).delete();

and in manifest must use these permissions

android.permission.WRITE_EXTERNAL_STORAGE

Rohit Jain

add below code in onDestroy method:

 if(myFile.exists())
 myFile.delete();

and don't forget to add permission in Manifest file

android.permission.WRITE_EXTERNAL_STORAGE

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!