图像未删除但file.delete()返回true?

时间:2018-04-13 11:33:22

标签: android android-file

我正在尝试使用file.delete()从设备中删除图片。正在调用Delete方法,即使它正在返回true,但是当我打开我的图库时,图像仍然存在。

我的图片路径是:

/data/user/0/com.myappname/app_myappname/20180413__164222_0.7680390806195996.png

请看看我的尝试。

1)

 File file = new File(path);
        if (file.exists()) {
            file.delete();
        }

2)

try {
 File file = new File("file://" + path);
        String getDirectoryPath = file.getParent(); // Only return path if physical file exist else return null
        File fileNew = new File(getDirectoryPath);
        fileNew.delete();
    } catch (Exception e) {
    }

3)

 File file = mContext.getFilesDir(); // this will get you internal directory path
            Log.d("BLA BLA", file.getAbsolutePath());
            File newfile = new File(file.getAbsolutePath() + imagePath); // foo is the directory 2 create
            newfile.delete();

4)

try
            {
                File sd = Environment.getExternalStorageDirectory();
                File data = Environment.getDataDirectory();

                if (sd.canWrite()) {
                    File fileName = new File(data, imagePath);
                    if (fileName.exists())
                    {
                        fileName.delete();
                    }
                }
            } catch (Exception e) {
            }

我已经应用了与file.delete()相关的堆栈溢出的几乎所有解决方案。但没有什么工作?任何帮助或线索将不胜感激。

编辑:

我在delete()使用此方法返回true时刷新了图库,但图片仍显示在应用图库中。

看看我的代码:

  public void callBroadCast() {
        if (Build.VERSION.SDK_INT >= 14) {
            Log.e("-->", " >= 14");
            MediaScannerConnection.scanFile(mContext, 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");
            mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                    Uri.parse("file://" + Environment.getExternalStorageDirectory())));
        }
    }

0 个答案:

没有答案