【问题标题】:Android read pdf file from asset folderAndroid从资产文件夹中读取pdf文件
【发布时间】:2018-01-19 16:06:34
【问题描述】:

我尝试读取资产文件夹中的 pdf 文件存储。我看到了这个解决方案: Read a pdf file from assets folder

但是就像 cmets 说的那样,它不再工作了,找不到文件,是否有另一种解决方案可以直接读取 pdf 文件,而无需在外部存储中复制 pdf 文件? 而且我不想使用 PdfRenderer 我的最小 API 是 17

【问题讨论】:

    标签: android pdf assets android-assets


    【解决方案1】:

    也许这对某人有帮助,所以我发布我的答案:

    private void openPdf(String pdfName){
        AssetManager assetManager = getAssets();
    
        InputStream in = null;
        OutputStream out = null;
        File file = new File(getFilesDir(), pdfName);
        try
        {
            in = assetManager.open(pdfName);
            out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);
    
            byte[] buffer = new byte[1024];
            int read;
            while ((read = in.read(buffer)) != -1)
            {
                out.write(buffer, 0, read);
            }
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
        } catch (Exception e)
        {
            Log.e("tag", e.getMessage());
        }
    
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(
                Uri.parse("file://" + getFilesDir() + "/"+pdfName),
                "application/pdf");
    
        startActivity(intent);
    }
    

    pdf 文件存储在 assets 文件夹中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-22
      • 2013-12-06
      相关资源
      最近更新 更多