Opening file from SD card via Intent.ACTION_VIEW

I know that we can open a file normally from internal storage like this:

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "my_file.zip");
Uri uri = (FileProvider.getUriForFile(context, AUTHORITY_OPEN_FILE, file)
Intent intent = new Intent(Intent.ACTION_VIEW)
            .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            .setData(uri);
context.startActivity(intent);

But I do not know on how to open a file from SD card, which we can pick using DocumentFile like this:

Uri uri = new Uri.Builder()
            .scheme("content")
            .authority("com.android.externalstorage.documents")
            .appendPath("document")
            .appendPath(directory)
            .appendPath(fileName)
            .build();
DocumentFile documentFile = DocumentFile.fromSingleUri(context, uri);

I tried the following snippet:

Uri uri = documentFile.getUri();
Intent intent = new Intent(Intent.ACTION_VIEW)
            .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            .setData(uri);
context.startActivity(intent);

But resulting error:

 Caused by: java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.VIEW dat=content://com.android.externalstorage.documents/document/6331-6132:/haxm-windows_r05_2.zip flg=0x10000001 cmp=com.google.android.gm/.browse.TrampolineActivity } from ProcessRecord{a0ed6d5 9894:com.mypackage.app/u0a169} (pid=9894, uid=10169) requires com.google.android.gm.permission.READ_GMAIL

I did grant for read and write external storage permissions:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

And Uri permission as well:

int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
context.getContentResolver().takePersistableUriPermission(uri, takeFlags);

A file stored in SD card gives us a Uri like this, where 6331-6132 is identifier for our removable SD card:

content://com.android.externalstorage.documents/document/6331-6132:Folder/haxm-windows_r05_2.zip

I read so many posts on StackOverflow, but nothing help. Can you help me to solve this error? Thanks in advance.

yo must add permission in manifest file and for marshmallow or above version of android you add run time permission. for this view

https://www.simplifiedcoding.net/android-marshmallow-permissions-example/

Opening file from SD card via Intent.ACTION_VIEW, yo must add permission in manifest file and for marshmallow or above version of android you add run time permission. for this view. java.lang.SecurityException: Permission Denial will be thrown when you open a external file using DocumentFile.fromSingleUri(context, uri). This static method is only useful when you pick a file inside onActivityResult() method with action Intent.ACTION_OPEN_DOCUMENT or Intent.ACTION_CREATE_DOCUMENT .

I get the Uri using Intent.ACTION_OPEN_DOCUMENT. Next, I save the Uri into my database. Finally, I reconstruct the Uri to get stream using getContentResolver().openInputStream(uri).

My guess is that you are not calling takePersistableUriPermission() on a ContentResolver in onActivityResult(), before you save the Uri to the database. Without this, you do not have long-term access to the content identified by the Uri.

How programmatically opening a pdf file stored in android , Author: balaji_3904a38c (balaji kunchapu) View Profile | View other solutions by this explains about how to open the PDF file which is stored on sdcard in Android intent=new Intent(Intent.ACTION_VIEW); Uri uri = Uri.fromFile(file); intent. Using this, you can write an app that lets you pick a file from the SD card and either displays the image or plays the music track as appropriate. Is the application available? If your app calls other apps as activities, you'll want to check that those other apps are available on your user's phone before trying to start them.

This may be very basic but the error message says that you are missing permission

did you ask for READ_EXTERNAL_STORAGE ?

https://developer.android.com/training/data-storage/files#ExternalStoragePermissions

Opening file from SD card via Intent.ACTION_VIEW, setData(uri); context.startActivity(intent); But I do not know on how to open a file from SD card, which we can pick using DocumentFile like this: Uri uri = new Uri. Intent.ACTION_GET_CONTENT to open mp3, play using MediaPlayer, tested on Android 5.1 Lollipop http://android-er.blogspot.com/2016/0

I finally found a solution for this. java.lang.SecurityException: Permission Denial will be thrown when you open a external file using DocumentFile.fromSingleUri(context, uri). This static method is only useful when you pick a file inside onActivityResult() method with action Intent.ACTION_OPEN_DOCUMENT or Intent.ACTION_CREATE_DOCUMENT. When your app is destroyed, the permission to access the file is gone. You should not use DocumentFile.fromSingleUri(context, uri) while working with files that require long time permission. Hence, we need DocumentFile.fromTreeUri(context, uri) instead.

To do that so, you must access the root of SD card's ID (e.g. 6331-6132) first. For example:

String path = "6331-6132:Video/Iykwim.mp4";
String sdcardId = path.substring(0, path.indexOf(':') + 1); // => returns '6331-6132:'
Uri uri = Uri.parse("content://com.android.externalstorage.documents/tree/" + Uri.encode(sdcardId));
DocumentFile root = DocumentFile.fromTreeUri(context, uri);

// Now, we already have the root of SD card.
// Next, we will get into => Video => Iykwim.mp4
DocumentFile file = root.findFile("Video").findFile("Iykwim.mp4");

Notice that you cannot access the file directly like this Uri:

String path = "6331-6132:Video/Iykwim.mp4";
Uri uri = Uri.parse("content://com.android.externalstorage.documents/tree/" + Uri.encode(path));
DocumentFile file = DocumentFile.fromTreeUri(context, uri);

Or like this:

String folder = "6331-6132:Video";
Uri uri = Uri.parse("content://com.android.externalstorage.documents/tree/" + Uri.encode(folder));
DocumentFile directoryVideo = DocumentFile.fromTreeUri(context, uri);
DocumentFile file = directoryVideo.findFile("Iykwim.mp4");

Finally, you can open the file using Intent.ACTION_VIEW:

Intent intent = new Intent(Intent.ACTION_VIEW)
            .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            .setData(documentFile.getUri());
context.startActivity(intent);

The worst scenario is when you call findFile() from a folder with so many files inside. It will take long time and may lead to crash your app. Ensure that you always call this method from different thread.

Android is getting worst day by day. They have made a Storage Access Framework (SAF) that makes you difficult to manage files on SD card. Using java.io.File is almost deprecated since Android Kitkat. You should use DocumentFile instead.

Snippet/Android: Open PDF file · GitHub, void onClick(View v) {. File file = new File("/sdcard/example.pdf");. if (file.exists​()) {. Uri path = Uri.fromFile(file);. Intent intent = new Intent(Intent.ACTION_VIEW);. SD cards are nice because you can make it accessible on your phone or you can pop it out and work with it on your PC. This step-by-step How-To shows you how to access your SD card through your

android.content.Intent.setDataAndType java code examples, Intent(Intent.ACTION_VIEW); intent. Intent.ACTION_VIEW); File file = new File(YOUR_SONG_URI); intent. How to open a PDF via Intent from SD card. File  Getting started. Open the door covering the SD card slot, insert a card that’s formatted using the FAT32 or exFAT file system, and you’re good to go.

Open file with default application using Intents, Apparently I need to copy the pdf to the sd card first then launch an Intent. I think the problem is the Intent launching so I'm simply trying to open a file "​example.pdf" that I copied onto the SD card using this code: ACTION_VIEW); intent. Meetups for people interested in making things with Raspberry Pi computers Thousands of clubs need volunteers like you Thanks to our community of thousands of volunteers around the world, over a million young people have already learned about digital making in a fun and accessible way.

Common Intents, An intent allows you to start an activity in another app by describing a simple To open a map, use the ACTION_VIEW action and specify the  I’ve read dozens of posts but am still stuck. Apparently I need to copy the pdf to the sd card first then launch an Intent. It still doesn’t work. I think the problem is the Intent launching so I’m simply trying to open a file “example.pdf” that I copied onto the SD card using this code:

Comments
  • "which we can pick using DocumentFile" -- how exactly are you doing this?
  • @CommonsWare, I have edited my question. DocumentFile has a static method to pick a file from SD card, i.e. DocumentFile.fromSingleUri(context, uri)
  • That does not pick a file from removable storage. That wraps a Uri that you obtained from somewhere in a DocumentFile. So, where did you get the Uri from? And are you saving the Uri somewhere outside of your process, such as in a database?
  • @CommonsWare, I get the Uri using Intent.ACTION_OPEN_DOCUMENT. Next, I save the Uri into my database. Finally, I reconstruct the Uri to get stream using getContentResolver().openInputStream(uri). I know that this method is success in reading and writing files. But I don't know how to open external app to handle the file.
  • @CommonsWare, For example, I have wrote a video file into sdcard using this stream: getContentResolver().openOutputStream(uri). Then I want external apps to open this file after file was wrote.
  • I did grant for read and write external storage permissions, and Uri permission as well. Read my question again.
  • I did that before. This error gives me headache for 3 days.
  • I called takePersistableUriPermission() after Intent.ACTION_OPEN_DOCUMENT_TREE calls onActivityResult().
  • I did grant for read and write external storage permissions, and Uri permission as well.