小编Mar*_*mel的帖子

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

我正在尝试使用其他应用程序打开文件,即打开带有Gallery的.jpg,带有Acrobat的.pdf等.

我遇到的问题是,当我尝试在应用程序中打开文件时,它只会打开所选应用程序而不是在应用程序中打开文件.我尝试通过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"));
}
Run Code Online (Sandbox Code Playgroud)

据我所知,它返回正确的URI和MIME类型: …

android android-intent

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

android ×1

android-intent ×1