简体   繁体   English

如何从资产子文件夹读取PDF文件

[英]How to read PDF files from assets subfolder

I am developing an education application which contains multiple test paper in the form of PDF file the PDF files are placed in Assets sub folder and showing in list view. 我正在开发一个教育应用程序,其中包含PDF格式的多个试卷,PDF文件放置在Assets子文件夹中并以列表视图显示。 to here my code is working fine. 到这里我的代码工作正常。 but when i am clicking on list items then the PDF file is not display showing error PDF file cannot be open (path error) 但是当我单击列表项时,则不会显示PDF文件,显示错误PDF文件无法打开(路径错误)

Thanks in Advance! 提前致谢!

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.pdfviewinfo);

     AssetManager asset = getAssets();
        try {
            final String[] arrdata = asset.list("pdffolder");
            List<String> pdflist = new ArrayList<String>();
            int size = arrdata.length;
            for(int i = 0;i<size;i++)
            {
              if(arrdata[i].contains(".pdf"))
              {
                pdflist.add(arrdata[i]); 
               }
            }
            ArrayAdapter<String> adapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,pdflist);
            ListView listView = (ListView) findViewById(R.id.listView1);
            listView.setAdapter(adapter);
                    listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
             public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                 valueinfo = (String)parent.getItemAtPosition(position); 
                 File file = new File("file:///android_asset/pdffolder/"+valueinfo);
              Log.i("testing", ""+file);
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.fromFile(file),"application/pdf");
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
            }
        });
    } catch (IOException e) {

        e.printStackTrace();
    }

     }
 }

Adobe Reader will not recognize the path when you keep the PDF File in your assets folder. 将PDF文件保存在资源文件夹中时,Adobe Reader将无法识别该路径。 When keeping PDF in your assets folder normally it will be available in your APK not on your External Storage so Adobe reader will not recognize the path. 通常,将PDF保留在资源文件夹中时,它将在APK中可用,而不在外部存储设备中可用,因此Adobe Reader无法识别该路径。 It should be kept in External Path to open it. 应该将其保存在“外部路径”中以将其打开。

Copy the files from assets folder to app private memory(so that the file is protected) and use file provider to give access to the pdfs files so that , pdf reader the external app can show pdf content. 将文件从资产文件夹复制到应用程序专用内存(以便保护文件),然后使用文件提供程序授予对pdfs文件的访问权限,以便pdf阅读器外部应用程序可以显示pdf内容。 https://developer.android.com/training/secure-file-sharing/setup-sharing.html https://developer.android.com/training/secure-file-sharing/setup-sharing.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM