【问题标题】:Check if an Uri from the FileProvider exist and is valid检查来自 FileProvider 的 Uri 是否存在且有效
【发布时间】:2018-02-24 01:51:42
【问题描述】:

我有一个这种格式的 Uri:

String myuri= "content://mypic/2017_01_mypic.jpg"

我想检查该文件是否存在于我的 android 应用程序存储库中。我试过这个:

File file = new File(URI.create(myuri))
if(file.exists()){//code here}

但它不起作用。我该如何解决这个问题?

【问题讨论】:

  • 使用ContentResolver
  • 应用存储库是什么意思?您是如何获得该内容方案的?

标签: android file android-studio uri


【解决方案1】:

您可以使用ContentResolver,就像使用 URI 获取路径一样。

 String[] projection = {MediaStore.MediaColumns.DATA};
    // Change the projection ap per need 
    Cursor cur = getContentResolver().query(Uri.parse(content_uri), projection, null, null, null);
    if (cur != null) {
        // File exist
    } else {
        // Invalid URI
    }

【讨论】:

  • 没有。没有好的代码。文件系统上不必有这样的文件。你开始没问题。但是如果光标不为空,那么你就准备好了。
猜你喜欢
  • 1970-01-01
  • 2018-02-11
  • 2010-12-19
  • 1970-01-01
  • 2011-02-24
  • 2019-07-11
  • 2017-12-18
  • 1970-01-01
  • 2011-06-17
相关资源
最近更新 更多