【问题标题】:android access pdf files from assests folderandroid从assets文件夹访问pdf文件
【发布时间】:2015-03-09 20:16:07
【问题描述】:

在我的应用程序中,我想将 PDF 文件存储在资产中,当按下查看按钮时,设备中安装的第 3 方应用程序应查看 PDF。

通过使用以下代码,我尝试访问资产文件夹中的 PDF 文件,但应用显示“文件不可用”。我在 stackoverflow 上搜索了各种代码,但没有一个有效。但是当我尝试提取 APK 文件时,我可以在 assets 文件夹中看到 PDF 文件。

以下是我试过的代码:

File file = new File("file://" + getFilesDir() + "/ccv.pdf");

            if (file.exists()) {

                Uri path = Uri.fromFile(file);
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(path, "application/pdf");
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                try {
                    startActivity(intent);
                } 
                catch (ActivityNotFoundException e) {
                    Toast.makeText(home.this, 
                        "No Application Available to View PDF", 
                        Toast.LENGTH_SHORT).show();
                }
            }
            else
            {
                Toast.makeText(home.this, 
                        "File not available", 
                        Toast.LENGTH_SHORT).show();
            }

所以请提供代码 sn-p 以访问 assets 文件夹中的 PDF 文件。

提前致谢

【问题讨论】:

    标签: android file pdf android-intent android-assets


    【解决方案1】:

    我终于找到了答案,使用 File 无法访问 Assets,只能使用 Assetmanager 访问它

    AssetManager assetManager = getAssets();
    InputStream ims = assetManager.open("ccv.pdf");
    

    【讨论】:

    • 接受你自己的答案,这样对别人有用。
    【解决方案2】:
    Try this
    
    File file = new File("file:///android_asset" + "/ccv.pdf");
    
                if (file.exists()) {
    
                    Uri path = Uri.fromFile(file);
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(path, "application/pdf");
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
                    try {
                        startActivity(intent);
                    } 
                    catch (ActivityNotFoundException e) {
                        Toast.makeText(home.this, 
                            "No Application Available to View PDF", 
                            Toast.LENGTH_SHORT).show();
                    }
                }
                else
                {
                    Toast.makeText(home.this, 
                            "File not available", 
                            Toast.LENGTH_SHORT).show();
                }
    

    【讨论】:

    • 你做什么你把那个pdf文件放在文件夹中或直接放在资产中,或者请使用意图中的直接链接尝试这个
    • 我已将文件放入 assets 文件夹中。这是截图:tinypic.com/r/2yvw7kk/8
    • 让我再检查一次,因为它在我的情况下工作正常等待
    • 能否请您删除if文件存在检查
    • 因为文件适用于外部存储而不是原始和资产文件夹
    【解决方案3】:

    我试图访问 assets 文件夹中的 PDF 文件

    为此,

    new File("file://" + getFilesDir() + "/ccv.pdf");  
    

    不会为您提供资产路径。

    getFilesDir()刚刚

    返回文件系统上目录的绝对路径 创建的文件

    要访问资产,请使用 getAssets()AssetManager

    您可以参考how-to-access-file-under-assets-folder-in-android

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-29
      • 2021-08-28
      • 2013-06-09
      • 2011-11-21
      • 1970-01-01
      • 1970-01-01
      • 2014-06-17
      • 2015-11-09
      相关资源
      最近更新 更多