android:删除图片

我从我的应用程序中删除图像文件。 我在做

new File(filename).delete (); 

这实际上是删除文件。 但是画廊里的形象依然可见。

在search我发现我们应该使用

getContentResolver().delete(Uri.fromFile(file), null,null); 删除

但在这里,我得到的例外:

未知的文件url。 java.lang.IllegalArgumentException:未知的URL文件:///mnt/sdcard/DCIM/Camera/IMG_20120523_122612.jpg

当我看到任何文件浏览器,这个特殊的图像是存在的。 请帮我解决这个问题。 有没有其他方式来更新图片时,图像被物理删除

Solutions Collecting From Web of "android:删除图片"

使用下面的代码,它可以帮助你。

 File fdelete = new File(file_dj_path); if (fdelete.exists()) { if (fdelete.delete()) { System.out.println("file Deleted :" + file_dj_path); } else { System.out.println("file not Deleted :" + file_dj_path); } } 

删除图片后使用下面的代码刷新图片发送广播

(对于<KITKAT API 14)

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

对于> = KITKAT API 14使用下面的代码

 MediaScannerConnection.scanFile(this, new String[] { Environment.getExternalStorageDirectory().toString() }, null, new MediaScannerConnection.OnScanCompletedListener() { /* * (non-Javadoc) * @see android.media.MediaScannerConnection.OnScanCompletedListener#onScanCompleted(java.lang.String, android.net.Uri) */ public void onScanCompleted(String path, Uri uri) { Log.i("ExternalStorage", "Scanned " + path + ":"); Log.i("ExternalStorage", "-> uri=" + uri); } }); 

因为:

 ACTION_MEDIA_MOUNTED 

在KITKAT(API 14)中已弃用。


编辑04-09-2015

其工作正常检查下面的代码

 public void deleteImage() { String file_dj_path = Environment.getExternalStorageDirectory() + "/ECP_Screenshots/abc.jpg"; File fdelete = new File(file_dj_path); if (fdelete.exists()) { if (fdelete.delete()) { Log.e("-->", "file Deleted :" + file_dj_path); callBroadCast(); } else { Log.e("-->", "file not Deleted :" + file_dj_path); } } } public void callBroadCast() { if (Build.VERSION.SDK_INT >= 14) { Log.e("-->", " >= 14"); MediaScannerConnection.scanFile(this, new String[]{Environment.getExternalStorageDirectory().toString()}, null, new MediaScannerConnection.OnScanCompletedListener() { /* * (non-Javadoc) * @see android.media.MediaScannerConnection.OnScanCompletedListener#onScanCompleted(java.lang.String, android.net.Uri) */ public void onScanCompleted(String path, Uri uri) { Log.e("ExternalStorage", "Scanned " + path + ":"); Log.e("ExternalStorage", "-> uri=" + uri); } }); } else { Log.e("-->", " < 14"); sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); } } 

下面是日志

 09-04 14:27:11.085 8290-8290/com.example.sampleforwear E/-->﹕ file Deleted :/storage/emulated/0/ECP_Screenshots/abc.jpg 09-04 14:27:11.085 8290-8290/com.example.sampleforwear E/-->﹕ >= 14 09-04 14:27:11.152 8290-8290/com.example.sampleforwear E/﹕ appName=com.example.sampleforwear, acAppName=/system/bin/surfaceflinger 09-04 14:27:11.152 8290-8290/com.example.sampleforwear E/﹕ 0 09-04 14:27:15.249 8290-8302/com.example.sampleforwear E/ExternalStorage﹕ Scanned /storage/emulated/0: 09-04 14:27:15.249 8290-8302/com.example.sampleforwear E/ExternalStorage﹕ -> uri=content://media/external/file/2416 

我已经看到很多答案,build议使用

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

这可以工作,但会导致媒体扫描仪重新扫描设备上的媒体。 更有效的方法是通过媒体商店内容提供商查询/删除:

 // Set up the projection (we only need the ID) String[] projection = { MediaStore.Images.Media._ID }; // Match on the file path String selection = MediaStore.Images.Media.DATA + " = ?"; String[] selectionArgs = new String[] { file.getAbsolutePath() }; // Query for the ID of the media matching the file path Uri queryUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; ContentResolver contentResolver = getContentResolver(); Cursor c = contentResolver.query(queryUri, projection, selection, selectionArgs, null); if (c.moveToFirst()) { // We found the ID. Deleting the item via the content provider will also remove the file long id = c.getLong(c.getColumnIndexOrThrow(MediaStore.Images.Media._ID)); Uri deleteUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id); contentResolver.delete(deleteUri, null, null); } else { // File not found in media store DB } c.close(); 
 File file = new File(photoUri); file.delete(); context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(photoUri)))); 

此代码适用于我,我认为它比重新安装整个SD卡Intent.ACTION_MEDIA_MOUNTED

要删除图像,

 ContentResolver contentResolver = getContentResolver(); contentResolver.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Images.ImageColumns.DATA + "=?" , new String[]{ imagePath }); 

我尝试了所有这些解决scheme,但在Android 6中没有运气。
最后,我发现这个代码工作正常。

 public static void 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)) { contentResolver.delete(uri, MediaStore.Files.FileColumns.DATA + "=?", new String[]{absolutePath}); } } } 

我也testing了这个在Android 4.4和5.1,它完美的作品。

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

这个代码有效,但是资源非常昂贵。 它卸载并安装可能会影响某些应用程序的SDCard或占用巨大的系统资源以刷新图库。 我仍然在寻找一个最好的替代品,并会发布,如果我得到一个。