简体   繁体   English

Android - 如何通过Intent在另一个应用程序中打开文件?

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

I'm trying to open a file using another app, ie opening a .jpg with Gallery, .pdf with Acrobat, etc. 我正在尝试使用其他应用程序打开文件,即打开带有Gallery的.jpg,带有Acrobat的.pdf等。

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. 我尝试通过Intent跟随Android打开pdf文件,但我必须遗漏一些东西。

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和MIME类型:

URI: file:///storage/emulated/0/Download/Katamari-ringtone-985279.mp3
MIME: audio/mpeg

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);
}

I used this piece of code and it worked perfectly fine on android 6 and below not tested on the higher version 我使用了这段代码,它在android 6及以下版本上工作得很好,没有在更高版本上测试过

public void openFile(final String fileName){
    Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri uri = Uri.fromFile(new File(android.os.Environment.getExternalStorageDirectory()
            .getAbsolutePath()+ File.separator+"/Folder/"+fileName));
    intent.setDataAndType(uri, "audio/mpeg");
    startActivity(intent);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过意图在另一个应用程序中打开本地应用程序文件? - How to open a local app file in another app via an intent? 如何通过Intent在Android中打开文件 - How to open a file in Android via an Intent 如何使用Android从Intent打开Paytm应用? - How do I open Paytm app from Intent using Android? Android Intent-如何打开文件 - Android Intent - How can i open file android:如何从我的应用程序打开另一个应用程序? - android: how do i open another app from my app? 如何通过Android应用程序打开PDF文件? - How can I open a PDF file via my Android app? Android通过Intent打开pdf文件 - Android open pdf file via Intent 如何将类文件变量获取到另一个活动,并使用意图而不是putExtra通过按钮进行访问? - How do I get class file variable to another activity and access via button using intent not putExtra? 如何为Chrome编写Android意图以打开GMail应用并搜索? - How do I write an Android intent for Chrome to open the GMail app and search? 当我的应用程序打开并且用户选择“始终”在我的应用程序中打开域时,如何绕过Android意向过滤器? - How do I bypass Android intent filter when my app is open and user has selected to “always” open domain in my app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM