无法通过编程方式从android的外部存储中删除文件

时间:2021-06-02 20:19:00

I am trying to delete a file located at the path

我正在删除路径上的一个文件

/storage/714D-160A/Xender/image/Screenshot_commando.png

What I've done so far:

到目前为止我所做的:

  try{
        String d_path = "/storage/714D-160A/Xender/image/Screenshot_commando.png";
        File file = new File(d_path);
        file.delete();

     }catch(Exception e){

        e.printStackTrace();
     }

and the file is still at its place(Not deleted :( )

且文件仍在原处(未删除:()

Also I've given permission in Manifest file.

我已经在Manifest文件中给出了权限。

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.STORAGE" />

3 个解决方案

#1


9  

Using ContentResolver to delete media files is wrong and provides many problems for the user.
You can not delete a file on the sd-card simply by deleting its information from the ContentResolver on android versions greater than Jelly Bean(4.3).
It works only on Android versions prior to KitKat(4.4).
That's why Android team provided DocumentProvider.

使用ContentResolver删除媒体文件是错误的,并且为用户提供了许多问题。您不能仅通过从大于Jelly Bean的android版本的ContentResolver中删除文件来删除sd卡上的文件(4.3)。它只适用于KitKat版本(4.4)之前的Android版本。这就是为什么Android团队提供DocumentProvider的原因。

Why contentResolver.delete(...) is wrong?
1. Fills up the sd-card
When you try to delete a media file on the sd-card by the ContentResolver on android versions greater than 4.3, the actual media file will remain untouched because the contentResolver.delete(...) approach only removes the information (name, date, path ...) of the media and you will end up having unregistered media files on your sd-card which ContentResolver has no idea about their existence anymore and that's why you couldn't see them in your gallery and you think they've been deleted with this approach while they're still there and fill up the sd-card each time the user tries to delete a media file on the sd-card.

为什么contentResolver.delete(…)是错误的?1。当android版本的ContentResolver试图删除sd卡上的媒体文件大于4.3时,实际的媒体文件将保持不变,因为ContentResolver .delete(…)方法只删除信息(姓名、日期、路径…)的媒体和你最终会有未登记的媒体文件在你的sd卡ContentResolver已经不知道它的存在,这就是为什么你看不到你的画廊,你认为他们已经被删除时使用这种方法仍然存在并填满sd卡每次用户试图删除一个sd卡上的媒体文件。

2. Media files (Images, videos, gifs ...) will come back to the gallery
There are many apps out there especially gallery and file manager ones that will find these unregistered media files and will add them to the ContentResolveragain as their normal behavior while the user assumes his/her unwanted media files are gone. Sure no user want's his/her assuming deleted images or videos show up in the middle of a demonstration.

2。媒体文件(图片、视频、gif…)会回到画廊有许多应用程序尤其是画廊和文件管理器,将会发现这些未登记的媒体文件,并将它们添加到ContentResolveragain正常行为,用户认为他/她多余的媒体文件已经一去不复返了。当然,如果在演示过程中出现被删除的图片或视频,没有用户想要。

So, what's the correct approach to remove media files on the sd-card?
Well, this has already been answered here with the use of DocumentProvider.

那么,删除sd卡上的媒体文件的正确方法是什么?这里已经用DocumentProvider方法来回答了这个问题。

#2


6  

public static boolean delete(final Context context, final File file) {
    final String where = MediaStore.MediaColumns.DATA + "=?";
    final String[] selectionArgs = new String[] {
            file.getAbsolutePath()
    };
    final ContentResolver contentResolver = context.getContentResolver();
    final Uri filesUri = MediaStore.Files.getContentUri("external");

    contentResolver.delete(filesUri, where, selectionArgs);

    if (file.exists()) {

        contentResolver.delete(filesUri, where, selectionArgs);
    }
    return !file.exists();
}

#3


3  

Use Environment.getExternalStorageDirectory().getAbsolutePath() instead of hard coding storage path

使用Environment.getExternalStorageDirectory().getAbsolutePath()代替硬编码存储路径

String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
File f = new File(baseDir + "/714D-160A/Xender/image/Screenshot_commando.png");
boolean d = f.delete();

#1


9  

Using ContentResolver to delete media files is wrong and provides many problems for the user.
You can not delete a file on the sd-card simply by deleting its information from the ContentResolver on android versions greater than Jelly Bean(4.3).
It works only on Android versions prior to KitKat(4.4).
That's why Android team provided DocumentProvider.

使用ContentResolver删除媒体文件是错误的,并且为用户提供了许多问题。您不能仅通过从大于Jelly Bean的android版本的ContentResolver中删除文件来删除sd卡上的文件(4.3)。它只适用于KitKat版本(4.4)之前的Android版本。这就是为什么Android团队提供DocumentProvider的原因。

Why contentResolver.delete(...) is wrong?
1. Fills up the sd-card
When you try to delete a media file on the sd-card by the ContentResolver on android versions greater than 4.3, the actual media file will remain untouched because the contentResolver.delete(...) approach only removes the information (name, date, path ...) of the media and you will end up having unregistered media files on your sd-card which ContentResolver has no idea about their existence anymore and that's why you couldn't see them in your gallery and you think they've been deleted with this approach while they're still there and fill up the sd-card each time the user tries to delete a media file on the sd-card.

为什么contentResolver.delete(…)是错误的?1。当android版本的ContentResolver试图删除sd卡上的媒体文件大于4.3时,实际的媒体文件将保持不变,因为ContentResolver .delete(…)方法只删除信息(姓名、日期、路径…)的媒体和你最终会有未登记的媒体文件在你的sd卡ContentResolver已经不知道它的存在,这就是为什么你看不到你的画廊,你认为他们已经被删除时使用这种方法仍然存在并填满sd卡每次用户试图删除一个sd卡上的媒体文件。

2. Media files (Images, videos, gifs ...) will come back to the gallery
There are many apps out there especially gallery and file manager ones that will find these unregistered media files and will add them to the ContentResolveragain as their normal behavior while the user assumes his/her unwanted media files are gone. Sure no user want's his/her assuming deleted images or videos show up in the middle of a demonstration.

2。媒体文件(图片、视频、gif…)会回到画廊有许多应用程序尤其是画廊和文件管理器,将会发现这些未登记的媒体文件,并将它们添加到ContentResolveragain正常行为,用户认为他/她多余的媒体文件已经一去不复返了。当然,如果在演示过程中出现被删除的图片或视频,没有用户想要。

So, what's the correct approach to remove media files on the sd-card?
Well, this has already been answered here with the use of DocumentProvider.

那么,删除sd卡上的媒体文件的正确方法是什么?这里已经用DocumentProvider方法来回答了这个问题。

#2


6  

public static boolean delete(final Context context, final File file) {
    final String where = MediaStore.MediaColumns.DATA + "=?";
    final String[] selectionArgs = new String[] {
            file.getAbsolutePath()
    };
    final ContentResolver contentResolver = context.getContentResolver();
    final Uri filesUri = MediaStore.Files.getContentUri("external");

    contentResolver.delete(filesUri, where, selectionArgs);

    if (file.exists()) {

        contentResolver.delete(filesUri, where, selectionArgs);
    }
    return !file.exists();
}

#3


3  

Use Environment.getExternalStorageDirectory().getAbsolutePath() instead of hard coding storage path

使用Environment.getExternalStorageDirectory().getAbsolutePath()代替硬编码存储路径

String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
File f = new File(baseDir + "/714D-160A/Xender/image/Screenshot_commando.png");
boolean d = f.delete();