Android 如何从SD卡打开pdf我可以从资产文件夹打开

Android 如何从SD卡打开pdf我可以从资产文件夹打开,android,pdf,android-sdcard,Android,Pdf,Android Sdcard,我使用的是pdf查看器库,能够显示资产文件夹中的pdf,但当我尝试从SD卡执行此操作时,它只是长时间显示progrss栏 我使用下面的代码从资产显示 AssetManager assetManager = getAssets(); InputStream in = null; OutputStream out = null; File file = new File(getFilesDir(), "sample.pdf"); try { i

我使用的是pdf查看器库,能够显示资产文件夹中的pdf,但当我尝试从SD卡执行此操作时,它只是长时间显示progrss栏

我使用下面的代码从资产显示

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

        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(this, MyPdfViewerActivity.class);
    intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, getFilesDir() + "/sample.pdf");
    startActivity(intent);

}

private void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
}}
我使用下面的代码从SD卡显示

    Intent intent = new Intent(this, MyPdfViewerActivity.class);
    intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME,
            "mnt/sdcard/sample.pdf");

/*  intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME,
            "assets/sample.pdf");*/

    startActivity(intent);
}

您可以尝试从SD卡访问PDF文件,如下所示:

File myPdfFile = new File(Environment.getExternalStorageDirectory(),"sample.pdf");
String path = myPdfFile.getAbsolutePath();

//Open the Pdf with this Method            
openPdfFileWithFilePath(path);

protected void openPdfFileWithFilePath(String path) {
// TODO Auto-generated method stub
 try {
        final Intent intent = new Intent(this, MyPdfViewerActivity.class);
        intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME,path);
        startActivity(intent);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

我希望这能有所帮助。

感谢您的快速响应,我没有尝试您的代码,因为我上面的代码工作正常。我得到的问题,因为我是在工作的刺激,但当我在实际设备上尝试它的工作很好。但仍然有问题。当我点击缩放时,它不能正确地显示文字放大znd znd znd znd znd znd znd znd znd znd znd znd znd znd znd znd znd znd znd znd znd znd znd znd ZN znd ZN ZN ZN ZN ZN ZN ZN ZN ZN ZN ZN ZN ZN Zthis@shivpaljodha看,伙计