【问题标题】:How can i get PDF Real path from Download Folder in Android? [duplicate]如何从 Android 的下载文件夹中获取 PDF 真实路径? [复制]
【发布时间】:2020-08-18 16:25:20
【问题描述】:

我想从下载文件夹中下载的 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 的真实路径,以便进一步使用。

提前致谢!!!

【问题讨论】:

  • 我也有确切的要求,你有没有以“msf:”id开头的解决方案?
  • 不,我没有得到任何解决方案,但我的问题已通过 uri 解决。

标签: java android pdf path uri


【解决方案1】:

无需获取“真实”路径。

没有必要。

按原样使用 uri。

这些天我们都这样。

加入我们。

【讨论】:

  • 不,我想要路径
  • 你为什么想要一个“路径”?一切都可以通过 uri 本身来完成。请详细说明。
  • 该路径需要在API中发送
  • 如果我需要通过 Javascript 调用获取文件怎么办?传递内容方案 uri 会导致错误:“Fetch API 无法加载内容://com.android.providers.downloads.documents/document‌​/...。不支持 URL 方案“内容”。”
  • 然后先复制到文件dystem。
猜你喜欢
  • 2020-02-05
  • 1970-01-01
  • 1970-01-01
  • 2019-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多