Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How do I open a file in another app via Intent?

I'm trying to open a file using another app, i.e. opening a .jpg with Gallery, .pdf with Acrobat, etc.

The problem I'm having with this is when I try to open the file in the app, it only opens the chosen app instead of opening the file within the app. I tried following Android open pdf file via Intent but I must be missing something.

public String get_mime_type(String url) {
    String ext = MimeTypeMap.getFileExtensionFromUrl(url);
    String mime = null;
    if (ext != null) {
        mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
    }
    return mime;
}

public void open_file(String filename) {
    File file = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DOWNLOADS), filename);

    // Get URI and MIME type of file
    Uri uri = Uri.fromFile(file).normalizeScheme();
    String mime = get_mime_type(uri.toString());

    // Open file with user selected app
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setData(uri);
    intent.setType(mime);
    context.startActivity(Intent.createChooser(intent, "Open file with"));
}

As far as I can tell, it returns the right URI and MIME type:

URI: file:///storage/emulated/0/Download/Katamari-ringtone-985279.mp3
MIME: audio/mpeg
like image 422
Martin Himmel Avatar asked Jul 24 '15 23:07

Martin Himmel


People also ask

How do I open a file with a specific app android?

Tap on the file and hold. Most file managers will open a menu where you can find an option like “Open with“. There, you can choose an app to open the file and make it the default by, in this case, ticking the box to remember this app.

How do I open one app from another app android?

Android App Development for Beginners Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken button view to open YouTube application.

How do I share files between apps on Android?

One way a sending app can share a file is to respond to a request from the receiving app. In all cases, the only secure way to offer a file from your app to another app is to send the receiving app the file's content URI and grant temporary access permissions to that URI.

How do I open another app activity?

If both application have the same signature (meaning that both APPS are yours and signed with the same key), you can call your other app activity as follows: Intent LaunchIntent = getActivity(). getPackageManager(). getLaunchIntentForPackage(CALC_PACKAGE_NAME); startActivity(LaunchIntent);


1 Answers

Posting my changes here in case it can help someone else. I ended up changing the download location to an internal folder and adding a content provider.

public void open_file(String filename) {
    File path = new File(getFilesDir(), "dl");
    File file = new File(path, filename);

    // Get URI and MIME type of file
    Uri uri = FileProvider.getUriForFile(this, App.PACKAGE_NAME + ".fileprovider", file);
    String mime = getContentResolver().getType(uri);

    // Open file with user selected app
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setDataAndType(uri, mime);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(intent);
}
like image 75
Martin Himmel Avatar answered Oct 05 '22 02:10

Martin Himmel