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

时间:2015-08-16 17:09:46

标签: java android file pdf assets

我一直在使用this PDF library,试图找到PDF文件所在的路径,但我一直收到错误。我在这里缺少什么?

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

enter image description here

感谢朋友们,这是我的错误,我刚刚发现我需要在打开文件之前将文件从资源读入文件目录。现在正在使用下面的代码

`     private 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();
        }

    }


}

`

1 个答案:

答案 0 :(得分:2)

没有&#34;资产文件夹&#34;在设备上,没有PDF文件所在的路径&#34;在设备上。

应用项目中public class TourListAdapter extends ArrayAdapter<Tour> { Context context; List<Tour> tours; public TourListAdapter(Context context, List<Tour> tours) { super(context, android.R.id.content, tours); this.context = context; this.tours = tours; } 文件夹的内容存储在APK中。您可以通过assets/访问资源,然后通过AssetManager上的getAssets()调用Context来获取其中一个资源,例如Activity

相关问题