android - 从项目中的原始文件夹中读取 .pdf

标签 android pdf pdf-reader

在这里,我试图从我的 Assets 文件夹中读取一个 .pdf 文件,但错误显示“无法打开此文档”。我尝试将 .pdf 文件复制到 SD 卡,然后从那里读取它,但没有成功。这是代码。请帮助我。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    File fileBrochure = new File("/sdcard/fleetman.pdf");
    if (!fileBrochure.exists())
    {
         CopyAssetsbrochure();
    } 

    /** PDF reader code */
    File file = new File("/sdcard/fleetman.pdf");        

    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);
    try 
    {
        getApplicationContext().startActivity(intent);
    } 
    catch (ActivityNotFoundException e) 
    {
         Toast.makeText(MainActivity.this, "NO Pdf Viewer", Toast.LENGTH_SHORT).show();
    }
}

//method to write the PDFs file to sd card
    private void CopyAssetsbrochure() {
        AssetManager assetManager = getAssets();
        String[] files = null;
        try 
        {
            files = assetManager.list("");
        } 
        catch (IOException e)
        {
            Log.e("tag", e.getMessage());
        }
        for(int i=0; i<files.length; i++)
        {
            String fStr = files[i];
            if(fStr.equalsIgnoreCase("fleetman.pdf"))
            {
                InputStream in = null;
                OutputStream out = null;
                try 
                {
                  in = assetManager.open(files[i]);
                  out = new FileOutputStream("/sdcard/" + files[i]);
                  copyFile(in, out);
                  in.close();
                  in = null;
                  out.flush();
                  out.close();
                  out = null;
                  break;
                } 
                catch(Exception e)
                {
                    Log.e("tag", e.getMessage());
                } 
            }
        }
    }

最佳答案

  try this, hope it ll help you 

  @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        CopyReadPDFFromAssets();

    }

    private void CopyReadPDFFromAssets()
    {
        AssetManager assetManager = getAssets();

        InputStream in = null;
        OutputStream out = null;
        File file = new File(getFilesDir(), "pdfdemofile.pdf");
        try
        {
            in = assetManager.open("pdfdemofile.pdf");
            out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

            copyPdfFile(in, out);
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
        } catch (Exception e)
        {
            Log.e("exception", e.getMessage());
        }

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(
                Uri.parse("file://" + getFilesDir() + "/pdfdemofile.pdf"),
                "application/pdf");

        startActivity(intent);
    }

    private void copyPdfFile(InputStream in, OutputStream out) throws IOException
    {
        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1)
        {
            out.write(buffer, 0, read);
        }
    }

在 list 文件中添加

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

关于android - 从项目中的原始文件夹中读取 .pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25541060/

相关文章:

php - 使用编码为 UTF-8 的 PDFTk 填充的 pdf 中缺少字符

java - 如何定位使用jasperreport生成的pdf中的位置来放置签名?

android - 在解析中,发送的推送始终为 0

android - 使用 flash 访问 Android 平板电脑的移动摄像头

javascript - PDF JavaScript 不再在 Edge 中运行

java - Pdf Reader 使用 java 在控制台中打印

javascript - 在 Node 中打开 PDF 时的错误处理

android - 如何在 Android 中注册 sleep 事件?

java - 如何将 Android SDK 添加到 IntelliJ

java - 如何使用 PDFBox 从 HTML 创建 PDF 文件?