简体   繁体   中英

How to get file path for pdf in android Marshmallow

I tried to get path from

 Intent intent = new Intent();             
            intent.setType("application/pdf");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(
                    Intent.createChooser(intent, "Complete action using"), 2);

But I did't get path from Marshamallow , I can get path from lollipop.

How I can get file path from internal storage?

I think you will have to use file provider for that. Refer this. https://developer.android.com/reference/android/support/v4/content/FileProvider

When you start an activity using startActivityForResult , the calling activity will get the results (if any) in onActivityResult . You should override that method in your calling activity...

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

}

This will method will get called as soon as the results are returned from the activity started by startActivityForResult . You can use the requestCode parameter to match (or filter out) the results you are expecting. In your case, you should check that requestCode is equal to 2 which is the value you provided in startActivityForResult .

If the requestCode is equal to 2, then you know that you need to process a PDF file. The intent parameter "could" have the path of that PDF file...

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    if(requestCode == 2 && data != null){
        //This is the uri to the file (which could be null)
        Uri uri = data.getData();

        if(uri != null){
            Log.i("LOG_TAG", String.format("uri path = %s", uri));
        }
    }

}

Now, to browse and select files you will do it the following way (Android Kitkat and above...I think)...

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/pdf");

startActivityForResult(intent, SELECT_FILE_REQUEST_CODE);

Then you receive the selected PDF uri as mentioned above

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