【问题标题】:How to get file path from content uri in android pie如何从android pie中的内容uri获取文件路径
【发布时间】:2019-08-17 16:24:54
【问题描述】:

我正在尝试使用改造 2.0 上传图像。在onActivityResult 中获取uri 后,我试图从uri 中获取路径,它给出了null。相同的代码在棒棒糖之前的设备中也能完美运行。

String[] filePathColumn = { MediaStore.Image.Media.DATA };
Cursor cursor = context.getContentResolver().query(inputUri,
                filePathColumn, null, null, null);
if (cursor != null) {
   cursor.moveToFirst();
   int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
   String filePath = cursor.getString(columnIndex);
   cursor.close();
// filePath is always null
   if (filePath != null && !filePath.isEmpty()) {
       file = new File(filePath);
   }
}

【问题讨论】:

  • 您是否为 Manifest 添加了权限?
  • 阅读本文android.jlelse.eu/…
  • @AntonisRadz 是的。我已经包含了权限。我之前使用过输入流,它正在工作。为了进行改造,我决定使用这种方法,但它不起作用。
  • @Mr.Bunny 我正在做文章中提到的所有事情。我之前使用输入流来获取文件。现在决定从 uri 中获取它,为此我使用上面给定的函数,但它不起作用。

标签: android nullpointerexception path uri android-9.0-pie


【解决方案1】:

这是从内容提供商处拍摄最后一张照片的完整工作代码:

      public ArrayList<String> getNewPicturesUri(JobParameters parameters) {
    ArrayList<String> newPictures = new ArrayList<>();

    if (parameters.getTriggeredContentAuthorities() != null) {
        if (parameters.getTriggeredContentUris() != null) {
            ArrayList<String> ids = new ArrayList<>();
            for (Uri uri : parameters.getTriggeredContentUris()) {
                List<String> path = uri.getPathSegments();
                if (path != null && path.size() ==
                        Media.EXTERNAL_CONTENT_URI.getPathSegments().size() + 1) {
                    ids.add(path.get(path.size() - 1));
                }
            }

            if (ids.size() > 0) {
                StringBuilder selection = new StringBuilder();
                for (int i = 0; i < ids.size(); i++) {
                    if (selection.length() > 0) {
                        selection.append(" OR ");
                    }
                    selection.append(MediaStore.Images.ImageColumns._ID);
                    selection.append("='");
                    selection.append(ids.get(i));
                    selection.append("'");
                }

                Cursor cursor = null;
                try {
                    String[] projection = new String[]{
                            MediaStore.Images.ImageColumns._ID,
                            MediaStore.Images.ImageColumns.DATA};

                    cursor = getContentResolver().query(
                            Media.EXTERNAL_CONTENT_URI,
                            projection, selection.toString(), null, null);

                    if (cursor != null) {
                        while (cursor.moveToNext()) {
                            newPictures.add(cursor.getString(PROJECTION_DATA));
                        }
                    }
                } catch (SecurityException exception) {
                    FirebaseCrash.report(exception);
                } finally {
                    if (cursor != null) {
                        cursor.close();
                    }
                }
            }
        }
    }
    return newPictures;
}

【讨论】:

  • 你能告诉我什么是 selection.toSrting(),同时获取光标。将其作为 null 传递,再次得到相同的错误。
  • 我更新了答案。我在 Job Service 中使用此方法来获取有关手机中新图片的通知。希望对你有帮助
  • 我应该在哪里使用从设备中选择文件后得到的 inputUri?
  • 你能告诉整个情况为什么你需要它并发布更多代码
  • 我发现了问题。使用 contentResolver,我们似乎无法直接获取路径。需要复制到缓存中再做。
猜你喜欢
  • 2011-03-01
  • 1970-01-01
  • 2020-06-10
  • 2014-07-31
  • 1970-01-01
  • 1970-01-01
  • 2012-08-15
  • 2017-05-08
  • 1970-01-01
相关资源
最近更新 更多