为了账号安全,请及时绑定邮箱和手机立即绑定

android:删除图像

/ 猿问

android:删除图像

当年话下 2019-11-14 15:26:28

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


new  File(filename).delete ();

这实际上是删除文件。但是图像在图库中仍然可见。


在搜索中,我发现我们应该使用


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


但是在这里我得到了例外:


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


当我使用任何文件浏览器查看时,都会显示此特定图像。请帮助我解决此问题。物理删除图片后,还有其他方法可以更新图库吗?


查看完整描述

3 回答

?
四季花海

使用下面的代码,可能会对您有所帮助。


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)中已弃用。


它的工作正常检查以下代码


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


查看完整回答
反对 回复 2019-11-14
?
幕布斯7119047

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


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();


查看完整回答
反对 回复 2019-11-14
?
慕尼黑的夜晚无繁华

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


查看完整回答
反对 回复 2019-11-14

添加回答

回复

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信