簡體   English   中英

無法以編程方式從android中的外部存儲中刪除文件

[英]can't delete file from external storage in android programmatically

我正在嘗試刪除位於路徑中的文件

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

到目前為止我所做的:

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

並且文件仍在原處(未刪除:()

我也在清單文件中授予了權限。

<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" />

使用ContentResolver刪除媒體文件是錯誤的,給用戶帶來了很多問題。
您不能簡單地通過從高於Jelly Bean(4.3) Android 版本的ContentResolver刪除其信息來刪除sd-card上的文件。
它僅適用於KitKat(4.4)之前的 Android 版本。
這就是 Android 團隊提供DocumentProvider的原因。

為什么contentResolver.delete(...)是錯誤的?
1.填滿sd-card
當您嘗試通過ContentResolver在高於 4.3 的 Android 版本上刪除sd-card上的媒體文件時,實際媒體文件將保持不變,因為contentResolver.delete(...)方法僅刪除信息(名稱、日期、路徑 ...),你最終會在你的sd-card上有未注冊的媒體文件, ContentResolver不知道它們的存在,這就是為什么你不能在你的畫廊中看到它們,你認為它們已經存在這種方法被刪除,而它們還在那里,並填補了sd-card每次用戶試圖刪除上的媒體文件sd-card

2. 媒體文件(圖片、視頻、gif...)將回到圖庫
有許多應用程序,尤其是圖庫和文件管理器,它們會找到這些未注冊的媒體文件,並將它們作為正常行為再次添加到ContentResolver ,而用戶認為他/她不需要的媒體文件已經消失。 當然,沒有用戶希望他/她假設已刪除的圖像或視頻出現在演示過程中。

那么,刪除sd-card上媒體文件的正確方法是什么?
好了,這已經回答了在這里與使用DocumentProvider

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

從Android 4.4開始,您不能使用正常方式寫入SD卡文件(App目錄除外)。 為此,您必須使用使用DocumentFile的存儲訪問框架。

以下代碼對我有用:

private void deletefile(Uri uri, String filename) {
    DocumentFile pickedDir = DocumentFile.fromTreeUri(this, uri);

    DocumentFile file = pickedDir.findFile(filename);
    if(file.delete())
        Log.d("Log ID", "Delete successful");
    else
        Log.d("Log ID", "Delete unsuccessful");
}

其中filename是要刪除的文件的名稱, uriACTION_OPEN_DOCUMENT_TREE返回的 URI:

private static final int LOCATION_REQUEST = 1;

private void choosePath() {
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
    intent.addCategory(Intent.CATEGORY_DEFAULT);
    startActivityForResult(intent, LOCATION_REQUEST);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
    if (requestCode == LOCATION_REQUEST && resultCode == Activity.RESULT_OK) {
        if (resultData != null) {
            Uri uri = resultData.getData();
            if (uri != null) {
                /* Got the path uri */
            }
        }
    }
}

使用Environment.getExternalStorageDirectory().getAbsolutePath()而不是硬編碼存儲路徑

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM