How to open private files saved to the internal storage using Intent.ACTION_VIEW?

You can’t share/send a file in internal storage, as referenced by a URI, via an Intent to another app. An app cannot read another app’s private data (unless it’s through a Content Provider). You pass the URI of the file in the intent (not the actual file itself) and the app that receives the intent needs to be able to read from that URI.

The simplest solution is to copy the file to external storage first and share it from there. If you don’t want to do that, create a Content Provider to expose your file. An example can be found here: Create and Share a File from Internal Storage

EDIT: To use a content provider:

First create your content provider class as below. The important override here is ‘openFile’. When your content provider is called with a file URI, this method will run and return a ParcelFileDescriptor for it. The other methods need to be present as well since they are abstract in ContentProvider.

public class MyProvider extends ContentProvider {

@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
    File privateFile = new File(getContext().getFilesDir(), uri.getPath());
    return ParcelFileDescriptor.open(privateFile, ParcelFileDescriptor.MODE_READ_ONLY);
}

@Override
public int delete(Uri arg0, String arg1, String[] arg2) {
    return 0;
}

@Override
public String getType(Uri arg0) {
    return null;
}

@Override
public Uri insert(Uri arg0, ContentValues arg1) {
    return null;
}

@Override
public boolean onCreate() {
    return false;
}

@Override
public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3,
        String arg4) {
    return null;
}

@Override
public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
    return 0;
}
}

Define your provider in the Manifest within the application tag, with exported=true to allow other apps to use it:

<provider android:name=".MyProvider" android:authorities="your.package.name" android:exported="true" />

In the openFile() method you have in your Activity, set up a URI as below. This URI points to the content provider in your package along with the filename:

Uri uri = Uri.parse("content://your.package.name/" + filePath);

Finally, set this uri in the intent:

intent.setDataAndType(uri, type);

Remember to insert your own package name where I have used ‘your.package.name’ above.

Leave a Comment