java - AndroidPdfViewer - 无法从 Assets 文件夹中打开 PDF 文档

标签 java android file pdf assets

我一直在与 this PDF library 合作,尝试获取 PDF 文件所在位置的路径,但我不断收到错误消息。我在这里缺少什么?

检查我的 Android 设备监视器,我可以在该数据库文件夹中看到我的数据库,该文件夹也驻留在 Assets 文件夹中,但我找不到所有 pdf 文件?请问他们在哪里?检查下面的图片

enter image description here

谢谢 friend ,这是我的错误,我刚刚发现我需要先将文件从 Assets 中读取到文件目录中,然后再打开它。现在正在使用下面的代码工作

` 私有(private)无效 CopyReadAssets(字符串文件名) { AssetManager assetManager = this.getAssets();

    InputStream in = null;
    OutputStream out = null;
    File file = new File(this.getFilesDir(), fileName);

    try
    {
        in = assetManager.open(fileName);
        out = this.openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

        Utility.copyFile(in, out);
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
    } catch (Exception e)
    {
        Log.e("tag", e.getMessage());
    }

    Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
    File dir_file = new File(file.getAbsolutePath());

    //<-- Get FileType
    String mimeType = Utility.getFileMineType(dir_file);

    intent.setDataAndType(Uri.fromFile(file), mimeType); //"file://" + getFilesDir() + "/abc.pdf"),"application/pdf"
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    if(mimeType.equals("application/pdf")){

        Log.e(TAG, Uri.fromFile(file).toString()+" -- "+dir_file);

        //mPDFView = new StudentPdfViewer();
        // mPDFView.display("Symfony.pdf", false);

        //handle it as activity
        Intent intent1 = new Intent(this, PdfViewer.class);
        intent1.putExtra(Config.COURSE_FILE, fileName);
        //intent1.putExtra(SqliteDb.COURSE_NAME,  listCategory.get(0).getCategory_name());
        //intent1.putExtras(mBundle);
        startActivity(intent1);

    }else{

        try {
            startActivity(intent);
        } catch (ActivityNotFoundException e) {

            Toast.makeText(this, "Unable to load selected Course Material, Please check the Study Studio installation guide to install the required dependency softwares", Toast.LENGTH_LONG).show();
        }

    }


}

`

最佳答案

设备上没有“ Assets 文件夹”,并且设备上没有“PDF 文件所在的路径”。

应用项目中 assets/ 文件夹的内容存储在 APK 内。您可以通过 AssetManager 访问资源,并且可以通过在任何方便的 Context(例如 Context)上调用 getAssets() 来获取其中之一> Activity

关于java - AndroidPdfViewer - 无法从 Assets 文件夹中打开 PDF 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32037793/

相关文章:

开发和部署期间的 Java 依赖项

android - 如何在我的 android 应用程序中实现搜索框?

java - SMSManager 和待处理 Intent

java - 安卓工作室 : message "Configure project in Project Structure dialog"

java - 如何避免在可观察的通知中使用 if/else?

android - 为什么在 CoordinatorLayout 中布局约束不起作用

c - 无效参数读写文件

file - Perforce P4 命令行 : How to find files in the repository?

java - 在使用 Java 读取文件时,如何从一行文本中提取名称和值?

java - 安装定制的共享库文件/或其任何替代方案