我一直在与this PDF library一起使用,试图获取PDF文件所在的路径,但是我一直遇到错误。我在这里想念什么?



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

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

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

`
    私有void CopyReadAssets(String fileName)
    {
        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();
        }

    }


}


`

最佳答案

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

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

07-24 18:58
查看更多