简体   繁体   English

如何在Android中通过Intent打开Pdf查看器

[英]How to open Pdf viewer via Intent in Android

VB.NET / SQL Developer new to Android development but concepts are getting through. VB.NET/SQL Developer是Android开发的新手,但概念正在逐渐普及。

I've managed to implement DownloadManager to download a PDF from a URI to DIRECTORY_DOWNLOADS folder but when I attempt to open with Intent I'm only prompted with Dropbox and Word not the normal applications .. code: 我已经设法实现DownloadManager来将URI中的PDF下载到DIRECTORY_DOWNLOADS文件夹,但是当我尝试使用Intent打开时,系统仅提示我使用Dropbox和Word,而不是普通应用程序..代码:

Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(Uri.parse(DIRECTORY_DOWNLOADS + File.separator + "WinLib" + File.separator + "WinLib.pdf"), "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);


try{
    startActivity(pdfIntent);
}catch(ActivityNotFoundException e) {
    Toast.makeText(MainActivity.this, "No Application available to view PDF", Toast.LENGTH_SHORT).show();
}

When I click on the file from the Downloads folder I'm presented with options for Drive PDF Viewer and OneDrive PDF Viewer. 当我从“下载”文件夹中单击文件时,会显示驱动器PDF Viewer和OneDrive PDF Viewer的选项。

As I said, I'm a noob to Android/Java so please don't be harsh but any help would be appreciated since Google has run dry on this one! 就像我说的那样,我对Android / Java不熟悉,所以请不要太苛刻,但是任何帮助都将不胜感激,因为Google在此方面干dry了!

Thanks in advance 提前致谢

Dave 戴夫

SOLUTION

User "Shine" pointed me in the direction of "Martin Himmel" post . 用户“ Shine”将我指向“ Martin Himmel” 帖子的方向。

I had to combine the intent.setData and intent.setType because when I used setType it reset the data to null so the file couldn't found. 我必须合并intent.setData和intent.setType,因为当我使用setType时,它将数据重置为null,因此找不到文件。

Once that was done I was getting the error: 完成后,我得到了错误:

android.os.FileUriExposedException: file:///storage/emulated/0/Download/WinLib/WinLib.pdf exposed beyond app through ClipData.Item.getUri()

The solution was found in "eranda.del"s post . 解决方案在“ eranda.del”的帖子中找到

So my final code looks like this: 所以我的最终代码如下所示:

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

    Uri uri = Uri.fromFile(file);
    String mime = get_mime_type(uri.toString());

    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
    StrictMode.setVmPolicy(builder.build());

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setDataAndType(uri, mime);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    this.startActivity(Intent.createChooser(intent, "Open file with"));
}

Thanks to Shine and the other contributors who helped me with this task, it's much appreciated! 感谢Shine和其他帮助我完成此任务的贡献者,非常感谢!

Dave 戴夫

Are you sure about that Uri.parse(DIRECTORY_DOWNLOADS + File.separator + "WinLib" + File.separator + "WinLib.pdf") parameter? 您确定该Uri.parse(DIRECTORY_DOWNLOADS + File.separator + "WinLib" + File.separator + "WinLib.pdf")参数吗? I would give a try to FileProvider.getUriForFile() with an approach similar to the one described here . 我将尝试使用类似于此处所述的方法来尝试FileProvider.getUriForFile() In this way, you would easily be able to both check pdf's Uri and mime type passed to the intent via: 这样,您就可以轻松地检查通过以下方式传递给意图的pdf的Uri和mime类型:

String mime = getContentResolver().getType(uri);

I also imagine .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) call to be required in your case, in order to grant read permission to the external viewing Intent. 我还想象在您的情况下需要.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)调用,以便将读取权限授予外部查看Intent。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM