获取多选PDF的真实路径

问题描述 投票:0回答:1

我有这个代码可以从设备中多选PDF,我需要每个PDF的真实路径,当我选择一个PDF时,我可以使用

获取真实路径
String path = data.getData().getPath();

但在多选的情况下,我只能获取每个选定 PDF 的 URI(据我所知),这是代码。

    private void selectPDFs() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("application/pdf");
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
        someActivityResultLauncher.launch(intent);
    }

    ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult>() {
                @Override
                public void onActivityResult(ActivityResult result) {
                    if (result.getResultCode() == Activity.RESULT_OK) {
                        Intent data = result.getData();
                        if (data != null) {
                            if (data.getClipData() != null) {
                                int count = data.getClipData().getItemCount();
                                int cItem = 0;
                                while (cItem < count) {
                                    String uri = Data.getClipData().getItemAt(cItem).getUri().toString();
                                    File f = new File(uri); // here i need the real path instead of uri
                                    files.add(f); // files is an ArrayList<File>
                                    cItem++;
                                }
                            } else if (data.getData() != null) {
                                String path = data.getData().getPath(); // path is the real file path
                                File f = new File(path);
                                files.add(f);
                            }
                        }
                    }
                }
            });
java android android-studio path uri
1个回答
0
投票

要从内容 URI 获取真实路径,可以使用 ContentResolver 查询 MediaStore 并获取文件路径。这是 onActivityResult 方法的更新版本:

@Override
public void onActivityResult(ActivityResult result) {
    if (result.getResultCode() == Activity.RESULT_OK) {
        Intent data = result.getData();
        if (data != null) {
            if (data.getClipData() != null) {
                int count = data.getClipData().getItemCount();
                int cItem = 0;
                while (cItem < count) {
                    Uri uri = data.getClipData().getItemAt(cItem).getUri();
                    String filePath = getRealPathFromUri(uri);
                    if (filePath != null) {
                        File f = new File(filePath);
                        files.add(f);
                    }
                    cItem++;
                }
            } else if (data.getData() != null) {
                String path = getRealPathFromUri(data.getData());
                if (path != null) {
                    File f = new File(path);
                    files.add(f);
                }
            }
        }
    }
}

private String getRealPathFromUri(Uri uri) {
    String[] projection = {MediaStore.Images.Media.DATA};
    Cursor cursor = getContentResolver().query(uri, projection, null, null, null);

    if (cursor != null) {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String filePath = cursor.getString(column_index);
        cursor.close();
        return filePath;
    } else {
        return uri.getPath(); // For non-MediaStore URIs, like FileProvider
    }
}

此 getRealPathFromUri 方法使用 ContentResolver 查询 MediaStore 并从内容 URI 获取真实文件路径。如果 URI 不是 MediaStore URI,它将回退到使用 URI 的路径。这应该适用于单选和多选。

© www.soinside.com 2019 - 2024. All rights reserved.