【问题标题】:AndroidPdfViewer - Can not open PDF document from Assets folderAndroidPdfViewer - 无法从 Assets 文件夹打开 PDF 文档
【发布时间】:2015-11-09 08:19:12
【问题描述】:

我一直在使用this PDF library,试图获取 PDF 文件所在的路径,但我一直收到错误消息。我在这里错过了什么?

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

谢谢朋友,这是我的错误,我刚刚发现我需要在打开文件之前将文件从资产读取到文件目录中。现在正在使用下面的代码工作

` 私人无效 CopyReadAssets(字符串文件名) { AssetManagerassetManager = 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();
        }

    }


}

`

【问题讨论】:

    标签: java android file pdf assets


    【解决方案1】:

    设备上没有“资产文件夹”,设备上也没有“PDF文件所在的路径”。

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

    【讨论】:

    • 这里的问题是当程序运行时我需要文件的 uri 路径驻留在 assets 文件夹中,但应用程序无法在该路径中找到任何文件,我检查我的 Android 设备监视器查看该目录中是否有任何文件但没有文件。这就是我使用的 `getFilesDir().getAbsolutePath() + "/"+ "sample.pdf" 我在这里遗漏了什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-09
    • 2013-06-09
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    • 2018-02-15
    相关资源
    最近更新 更多