java - 如何从 Android 中的下载文件夹获取 PDF 真实路径?

标签 java android pdf path uri

我想从下载文件夹中下载的 pdf 的 URI 获取 PDF 的真实路径。

我已经在关注 this关联。但是在这方面,我没有得到真正的路径并得到异常。如下所示:

URI

content://com.android.providers.downloads.documents/document/msf%3A26

午餐意向:

 Intent intent = new Intent();
 intent.setType("application/pdf");
 intent.setAction(Intent.ACTION_GET_CONTENT);
 startActivityForResult(Intent.createChooser(intent, "Select PDF"), PICK_PDF_REQUEST);

OnActivityResult:

if (requestCode == PICK_PDF_REQUEST) {
            if (resultCode == Activity.RESULT_OK) {
                Uri uri = data.getData();
                String path = null;
                try {
                    path = getPDFPath(uri);
                    LogUtils.logE(TAG, "Document :  " + path);
                } catch (URISyntaxException e) {
                    e.printStackTrace();
                }
          }
 }


     private String getPDFPath(Uri uri) {
            // DocumentProvider
            if (DocumentsContract.isDocumentUri(context, uri)) {
                // ExternalStorageProvider
                if ("com.android.externalstorage.documents".equals(uri.getAuthority())) {
                    String documentId = DocumentsContract.getDocumentId(uri);
                    String[] split = documentId.split(":");
                    String type = split[0];
                    return Environment.getExternalStorageDirectory().toString() + "/" + split[1];

                } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
                    String decodedURI = Uri.decode(uri.toString());

                    if (decodedURI.contains("raw:")) {
                        return decodedURI.substring(decodedURI.indexOf("raw:") + 4);
                    }

                    String id = DocumentsContract.getDocumentId(Uri.parse(decodedURI));

                    Uri contentUri = ContentUris.withAppendedId(
                            Uri.parse("content://downloads/public_downloads"), java.lang.Long.valueOf(id));

                    return getDataColumn(contentUri, null, null);
                }
            }
            return null;
        }

        public String getDataColumn(Uri uri, String selection,
                                    String[] selectionArgs) {

            Cursor cursor = null;
            final String column = "_data";
            final String[] projection = {
                    column
            };

            try {
                cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
                        null);
                if (cursor != null && cursor.moveToFirst()) {
                    final int index = cursor.getColumnIndexOrThrow(column);
                    return cursor.getString(index);
                }
            } finally {
                if (cursor != null)
                    cursor.close();
            }
            return null;
        }

异常

 2020-05-04 14:47:09.747 17908-17908/ W/System.err: java.lang.NumberFormatException: For input string: "msf:26"
2020-05-04 14:47:09.748 17908-17908/W/System.err:     at java.lang.Long.parseLong(Long.java:594)
 2020-05-04 14:47:09.748 17908-17908/ W/System.err:     at java.lang.Long.valueOf(Long.java:808)

我想要 PDF 的真实路径,以便进一步使用它。

提前致谢!!!

最佳答案

无需获得“真实”路径。

没有必要。

只需按原样使用 uri。

现在我们都这样。

加入我们。

关于java - 如何从 Android 中的下载文件夹获取 PDF 真实路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61588924/

相关文章:

java - 如何按输入顺序保存和检索共享首选项中的数据?

javascript - 仅使用 HTML 或 Javascript 从 Android WebView 在默认浏览器中打开链接

java - 我收到 java.lang.NoClassDefFoundError : org/bouncycaSTLe/asn1/ASN1Primitive error

windows - 直接从dos命令行打印pdf文件

javascript - 使用 Google Apps 脚本将两个选项卡合并到一个 pdf 中时出现 "ReferenceError: setTimeout"

java - 使用 long 计算素数 (Java)

java - 弃用分页参数

java - 有没有更好的方法将日历日期归零?

android - JdwpPaquet 中的 NullPointerException

java - ClassCastException : Fragment cannot be cast to Fragment