如何使用Uri从外部存储android studio中删除文件

时间:2018-02-15 17:45:45

标签: android android-studio video 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",根据我的打印logcat,找不到此文件。注意:此文件确实存在,我使用相同的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转换为有用的东西。我不想手动输入我的文件位置的地址。

@Here是我的整个代码......

公共课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;
}

}

2 个答案:

答案 0 :(得分:0)

  

所以我看了一些其他人的例子并实现了这个。

只有当Uri具有file方案并且您对包含该文件的目录具有写入权限时,这才有效。

  

如何使用Uri

从外部存储android studio中删除文件

最有可能的是,你没有。

如果您从UriACTION_OPEN_DOCUMENT获得ACTION_CREATE_DOCUMENT,请使用UriDocumentFile打包在fromSingleUri()中,然后致电{{ 1 {} delete()

对于任何其他DocumentFile content,欢迎您尝试在Uri上致电delete(),但不要指望它可以正常工作。不要求ContentResolver为您提供删除内容的任何方式。提供ContentProvider的应用程序应该有自己的方式允许用户删除内容。

答案 1 :(得分:0)

因此,对于那些不想阅读我们评论的人来说,这是我的问题的解决方案。首先,选择要下载的目录。然后使用该文件路径并使用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);

      }
}