简体   繁体   English

在显示文件之前立即关闭意图打开 PDF

[英]Intent to open PDF instantly closes before showing the file

I am using the following code to open a specific file I have placed in my working app module...我正在使用以下代码打开放置在我的工作应用程序模块中的特定文件...

private void openPDF(String filename) {

    File file = new File(Environment.getExternalStorageDirectory(), filename);
    Uri path = Uri.fromFile(file);

    Intent pdfIntent = new Intent(Intent.ACTION_VIEW);

    pdfIntent.setDataAndType(path, "application/pdf");

    try {
        startActivity(pdfIntent);
    } catch (ActivityNotFoundException e) {
        Toast.makeText(MainActivity.this, "There is no PDF file.",
                       Toast.LENGTH_SHORT).show();
    }
}

I get within the try-statement and the startActivity(...) method is invoked.我进入了try-statement并调用了startActivity(...)方法。 I get a dialog to choose which app I want to open the file in, but when I choose the app, Android will open the app, display the correct filename within the app, but then will automatically close the PDF app before the file is actually displayed.我得到一个对话框来选择我想在哪个应用程序中打开文件,但是当我选择该应用程序时,Android 将打开该应用程序,在应用程序中显示正确的文件名,但会在文件实际打开之前自动关闭 PDF 应用程序显示。

How do I go about keeping the PDF file open until the user decides to actually "close" it?在用户决定实际“关闭”它之前,我如何保持 PDF 文件打开?

By the way, I get no error messages and no warnings that I can see.顺便说一下,我没有收到任何错误消息,也没有我可以看到的警告。 The PDF app just closes. PDF 应用程序刚刚关闭。 I also, tried to open the file with Google Custom Tabs since I already use that in my app, but it didn't open the PDF file (as I kind of expected)我也尝试使用Google Custom Tabs打开文件,因为我已经在我的应用程序中使用了它,但它没有打开 PDF 文件(正如我所料)

Update更新

For implementing StreamProvider , I have decided to create a simple extension to StreamProvider that will read a unique tag pdf-path that will delegate the work to open an asset file为了实现StreamProvider ,我决定创建一个简单的扩展StreamProvider将读取的唯一标签pdf-path ,将工作委托给打开asset文件

Here is the code...这是代码...

public class PdfProvider extends StreamProvider {

    // Special xml path tag that will work for PDFs (or any asset really)
    private static final String PDF_TAG = "pdf-path";

    @Override
    protected String getUriPrefix() { return null; }

    @Override
    protected StreamStrategy buildStrategy(Context context, String tag, String name, String path, HashMap<String, String> attrs) throws IOException {
        if (PDF_TAG.equals(tag)) {
            super.buildStrategy(context, "asset", name, path, attrs);
        }
        return super.buildStrategy(context, tag, name, path, attrs);
    }
}

Also, I have the noCompress addition to my gradle file and the path meta-data...另外,我的gradle文件和路径元数据中添加了noCompress ...

<paths>
    <pdf-path name="resume" path="resume/resume.pdf"/>
</paths>

I am confused at how to actually use PdfProvider .我对如何实际使用PdfProvider感到困惑。 The documentation seems to have asset logic as a second-thought and the demo code for StreamProvider is a little convoluted with additional logic that makes it harder to understand the provider aspect.该文档似乎将asset逻辑作为第二个想法, StreamProvider的演示代码与附加逻辑有点复杂,这使得理解提供者方面变得更加困难。

I just drag-and-dropped the file within the app module.我只是将文件拖放到应用程序模块中。 Not within the src folder.不在 src 文件夹中。 Is that even right?甚至这样吗?

Alas, no.唉,没有。 That is your project on your development machine.那是您在开发机器上的项目。 It is not external storage on ~1.5 billion devices.它不是大约 15 亿设备上的外部存储

If your long-term objective is to actually show a PDF from external storage, put it on external storage of your device or emulator.如果您的长期目标是从外部存储实际显示 PDF,请将其放在设备或模拟器的外部存储上。 With the Android Studio 2.x emulator, you can drag and drop the PDF into the emulator window, though that will put it in the Downloads/ directory of external storage, not the root.使用 Android Studio 2.x 模拟器,您可以将 PDF 拖放到模拟器窗口中,但这会将它放在外部存储的Downloads/目录中,而不是根目录中。

If your long-term objective is to show a PDF that you package with your app, put the PDF in assets/ of your module's main sourceset (eg, app/src/main/assets/ in a typical Android Studio project).如果您的长期目标是显示与应用程序一起打包的 PDF,请将 PDF 放在模块main assets/中(例如,典型 Android Studio 项目中的app/src/main/assets/ )。 Then, either:然后,要么:

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

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