【问题标题】:How to get real path of capture image and gallery image in android in android 10 and above如何在android 10及更高版本的android中获取捕获图像和图库图像的真实路径
【发布时间】:2021-01-09 14:40:20
【问题描述】:

为了获取捕获图像和图库图像的真实路径,以下代码对我来说非常好。

private String getRealPathFromURI(Uri contentUri) {
        String result = null;
        try{
            String[] proj = { MediaStore.Images.Media.DATA };
            CursorLoader loader = new CursorLoader(requireActivity(), contentUri, proj, null, null, null);
            Cursor cursor = loader.loadInBackground();
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            result = cursor.getString(column_index);
            cursor.close();
        }catch(Exception e){
            Log.i("error_getting_path",e.getMessage());
        }

        return result;
    }

但在 android 10 及更高版本的设备中,我无法获取捕获和图库图像的路径。 所以我的应用无法在 android 10 及更高版本中运行此功能。 请建议在所有 android 设备中获取路径的最佳方法。

【问题讨论】:

  • Android 10 和 11 上不使用 .DATA 列。最好的方法不是尝试从一个不错的 uri 获取路径(无论如何都是一个讨厌的习惯),而是直接使用 uri。你不需要路径……嗯……基本上不需要。
  • 文件 file = new File(getRealPathFromURI(contentUri)); RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file); RequestBody requestBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("image",file.getName(),requestFile) .build();
  • 这是我使用所选路径上传文件或捕获图像的方法,我如何在没有文件路径的情况下执行此操作。从 uri 创建的文件不适用于 .. @blackapps
  • 有类似 .addStreamUri 的东西.....抱歉忘记了名字。看一看。 ide 给你一个选择。

标签: java android image android-intent gallery


【解决方案1】:
  private static String getRealPathFromURI(Uri uri) {
    Uri returnUri = uri;
    Cursor returnCursor = getContentResolver().query(returnUri, null, null, null, null);
    int nameIndex = returnCursor.getColumnIndex( OpenableColumns.DISPLAY_NAME);

    returnCursor.moveToFirst();
    String name = (returnCursor.getString(nameIndex));

    File file = new File(context.getFilesDir(), name);
    try {
        InputStream inputStream = context.getContentResolver().openInputStream(uri);
        FileOutputStream outputStream = new FileOutputStream(file);
        int read = 0;
        int maxBufferSize = 1 * 1024 * 1024;
        int bytesAvailable = inputStream.available();

      
        int bufferSize = Math.min(bytesAvailable, maxBufferSize);

        final byte[] buffers = new byte[bufferSize];
        while ((read = inputStream.read(buffers)) != -1) {
            outputStream.write(buffers, 0, read);
        }
      
        inputStream.close();
        outputStream.close();
        Log.e("File Path", "Path " + file.getAbsolutePath());

    } catch (Exception e) {
        Log.e("Exception", e.getMessage());
    }
    return file.getAbsolutePath();
}

【讨论】:

  • 这不起作用,因为 .DATA 列将保持为空。
  • 这在我的演示中工作你必须在应用程序标签内的 mainfest 中使用 android:requestLegacyExternalStorage="true" 并且你添加了文件提供程序
  • This has no effect as the .DATA column will stay null.
  • 我在哪里使用了我没用过的数据,你能检查一下我的方法并用你的替换吗
  • 可以。但这是荒谬的代码,因为您首先制作文件的副本,然后返回副本的路径而不是原始文件的路径。然后假装你得到了“真正的道路”。将其命名为 getPathOfACopy。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-06
相关资源
最近更新 更多