简体   繁体   中英

Unable to get pdf path in android 9

Device - Moto G7
Android OS - Android Pi

I am unable to get pdf path when selecting pdf from recent folder. When I am selection same pdf from actual folder, its working fine.

Any help?

below code I have used to get PDF Path

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static String getPathFromURI(final Context context, final Uri uri) {

    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

    // DocumentProvider
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
        // ExternalStorageProvider
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            if ("primary".equalsIgnoreCase(type)) {
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            }
        }
        // DownloadsProvider
        else if (isDownloadsDocument(uri)) {
            final String id = DocumentsContract.getDocumentId(uri);
            if (id.startsWith("raw:")) {
                return id.replaceFirst("raw:", "");
            }

            String fileName = getDataColumn(context, uri, null, null);
            String uriToReturn = null;
            if (fileName != null){
                uriToReturn = Uri.withAppendedPath(Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()), fileName).toString();
                return uriToReturn;
            }

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

            String[] contentUriPrefixesToTry = new String[]{
                    "content://downloads/public_downloads",
                    "content://downloads/my_downloads",
                    "content://downloads/all_downloads"
            };
            String path = "";
            for (String contentUriPrefix : contentUriPrefixesToTry) {
                Uri contentUri = ContentUris.withAppendedId(Uri.parse(contentUriPrefix), Long.valueOf(id));
                //DocumentFile tempPath = DocumentFile.fromSingleUri(context,contentUri);
                try {
                    path = getDataColumn(context, contentUri, null, null);
                    if (path != null) {
                        return path;
                    }
                } catch (Exception e) { }

            }

//          return getDataColumn(context, contentUri, null, null);
            return path;

        }
        // MediaProvider
        else if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            Uri contentUri = null;
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }

            final String selection = "_id=?";
            final String[] selectionArgs = new String[]{
                    split[1]
            };

            return getDataColumn(context, contentUri, selection, selectionArgs);
        }
    }
    // MediaStore (and general)
    else if ("content".equalsIgnoreCase(uri.getScheme())) {
        return getDataColumn(context, uri, null, null);
    }
    // File
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

You can use FilePicker to get files from internal storage

FilePickerBuilder.getInstance().setMaxCount(10 - filesUploadedPathList.size())
                        .setActivityTheme(R.style.AppTheme)
                        .pickFile(FilesUploadFragment.this);

Now in onActivityResult only you have to do this

if (data.getStringArrayListExtra(FilePickerConst.KEY_SELECTED_DOCS) != null && data.getStringArrayListExtra(FilePickerConst.KEY_SELECTED_DOCS).size() > 0)

{ filesUploadedPathList.addAll(data.getStringArrayListExtra(FilePickerConst.KEY_SELECTED_DOCS)); }

fileUploadedpathlist will be a list of Strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM