首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从Android的下载文件夹中获得PDF真实路径?

如何从Android的下载文件夹中获得PDF真实路径?
EN

Stack Overflow用户
提问于 2020-05-04 09:22:58
回答 1查看 1.8K关注 0票数 3

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

我已经在跟踪this链接。但在这一点上,我没有得到真正的路径和异常。如下所示:

URI

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

午餐意向:

代码语言:javascript
复制
 Intent intent = new Intent();
 intent.setType("application/pdf");
 intent.setAction(Intent.ACTION_GET_CONTENT);
 startActivityForResult(Intent.createChooser(intent, "Select PDF"), PICK_PDF_REQUEST);

OnActivityResult:

代码语言:javascript
复制
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;
        }

异常

代码语言:javascript
复制
 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的真实路径,所以我可以使用它进一步使用。

提前谢谢!

EN

回答 1

Stack Overflow用户

发布于 2020-05-04 09:44:56

不需要走一条“真正的”道路。

没有必要这样做。

只需按原样使用uri。

我们这些天都喜欢这样。

加入我们吧。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61588924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档