簡體   English   中英

如何僅使用Uri從外部存儲Android Studio中刪除文件

[英]How to delete a file from external storage android studio, using only Uri

因此,我查看了其他人的示例並實施了該示例。

 File delete= new File(uri.getPath());
    if(delete.exists()){
        Log.d("Delete", "File Exists");
        if(delete.delete()){
            Log.d("Deleted", ""+uri);
        }
        else{
            Log.d("Unable to delete", ""+uri);
        }
    }
    else{
        Log.d("Delete", "File is not found");
    }

現在唯一的問題是我得到的路徑名是“ content:// downloads / all_downloads / 644”,並且根據我打印的日志記錄,找不到此文件。 注意:此文件確實存在,我使用了相同的uri來實際播放視頻。 謝謝您的幫助。 編輯:好的,這就是我獲取URI的方式。

BroadcastReceiver onComplete = new BroadcastReceiver() {

    public void onReceive(Context ctxt, Intent intent) {
        Log.d("REFERENCE", "E"+"Entering Broadcast");
        long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
        uri=( manager.getUriForDownloadedFile(referenceId));
    }
};

現在,對於以下給出的建議,我非常感謝,但是即使閱讀了文檔,仍然感到困惑。 假設我無法使用Uri刪除,有沒有一種方法可以將我的uri轉換為有用的東西。 我不想手動輸入文件位置的地址。

@這是我的整個代碼...

公共課程Cacher {

private DownloadManager manager;
private Uri uri;

public Cacher(String urlIn , Context context){

    Log.d("REFERENCE", "A1- casher");
    manager= (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    Log.d("REFERENCE", "A");
    Uri temp=Uri.parse(urlIn);
    Log.d("REFERENCE", "B");
    DownloadManager.Request request= new DownloadManager.Request(temp);
    //
    request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    Long reference=manager.enqueue(request);
    // Toast.makeText(this,""+reference,Toast.LENGTH_LONG);
    Log.d("REFERENCE", "C"+"Downloading video");
    Log.d("REFERENCE", "D"+"Setting broadcast");
    context.registerReceiver(onComplete,
            new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    Log.d("REFERENCE", "F"+"Now Setting the uri table with");
}

BroadcastReceiver onComplete = new BroadcastReceiver() {

    public void onReceive(Context ctxt, Intent intent) {
        Log.d("REFERENCE", "E"+"Entering Broadcast");
        long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
        uri=( manager.getUriForDownloadedFile(referenceId));

    }
};
//TODO: Add Utility to remove all Cached Videos
//TODO: Add Utility to delete a single cached video after it has been watched.

public Uri getUri() {
    return uri;
}

}

因此,我查看了其他人的示例並實施了該示例。

僅當Uri具有file方案並且您對包含文件的目錄具有寫訪問Uri這才起作用。

如何僅使用Uri從外部存儲Android Studio中刪除文件

您很可能不會。

如果從ACTION_OPEN_DOCUMENTACTION_CREATE_DOCUMENT獲取Uri ,請使用fromSingleUri()Uri包裹在DocumentFile ,然后在DocumentFile上調用delete()

對於其他任何Uri content ,歡迎您嘗試在ContentResolver上調用delete() ,但不要期望它能正常工作。 不需要ContentProvider為您提供任何刪除內容的方法。 提供ContentProvider的應用程序應具有自己的允許用戶刪除內容的方式。

因此,對於那些不想閱讀我們評論的人來說,這是我的問題的解決方案。 首先,選擇一個目錄下載到。 然后使用該文件路徑並使用delete()功能。 請記住,一旦使用文件夾名稱創建目錄,您只需要引用文件夾名稱,而不是整個路徑。 這是我希望對您有所幫助的代碼:

    String in= GenerateDirectory.createVideoDirectory(context);// Might be useless here.
    manager= (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    Uri temp=Uri.parse(urlIn);
    Ringtone r= RingtoneManager.getRingtone(context, temp);
    String filename=(r.getTitle(context));
    DownloadManager.Request request= new DownloadManager.Request(temp);
    request.setDestinationInExternalPublicDir("/secretVideos", filename);
    uri=Uri.parse(in+"/"+filename);
    Log.d("REFERENCE", "Uri that is returned "+uri);
    request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    manager.enqueue(request);

// 接着

 public void removeCache(){
    //TODO: Fix this. For some reason the Uri used gives invalid path name.
    Log.d("Delete", "Directory to find ... "+uri);
    File delete= new File(uri.getPath());
    if(delete.exists()){
        Log.d("Delete", "File Exists");
        if(delete.delete()){
            Log.d("Deleted", ""+uri);
        }
    }
}

//我在哪里創建目錄

 private int RESULT=0;
  public static String createVideoDirectory(Context context){
      String pathToExternalStorage = Environment.getExternalStorageDirectory().toString();
      File appDirectory = new File(pathToExternalStorage + "/" + "secretVideos");
      if(!appDirectory.exists()){
          Log.d("Directory","Directory DNE");
            if(appDirectory.mkdir()) {
             Log.d("Directory", "Directory  Created");

             //Log.d("Directory","Unable to create directory, using default directory");
            }
            else if(appDirectory.canWrite()){
                Log.d("Directory", "Permission given");
      }
            else{
                Log.d("Directory", "Lack of permission");
            }
      }

      else{
          Log.d("Directory","Already Exists");
      }


    return appDirectory.toString();
}
private void writeAndReadPermissions(Context context){
      if(ContextCompat.checkSelfPermission(context, permission.WRITE_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions((Activity) context, new String[]{permission.WRITE_EXTERNAL_STORAGE, permission.READ_EXTERNAL_STORAGE}, RESULT);

      }
}

暫無
暫無

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

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