从Raw文件夹打开pdf文件时出现问题

时间:2017-09-15 05:13:52

标签: android pdf

您好我使用以下代码从原始文件夹中打开.pdf文件,该文件位于 res 文件夹中。

final Intent imageintent = new Intent(android.content.Intent.ACTION_VIEW);
imageintent.setDataAndType(Uri.parse("android.resource://com.myplayback/"+R.raw.demo), "application/pdf");
startActivity(imageintent);

它给出了以下错误:

 Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=android.resource://com.myplayback/2131099648 typ=application/pdf }

应该是什么问题? 告诉我是否有其他选项可用于从原始文件夹中读取pdf文件注意:我已经完成了stackoverflow上提供的解决方案,但没有成功。感谢。

修改

public class Main2Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    File fileBrochure = new File(Environment.getExternalStorageDirectory() + "/" + "demo.pdf");
    if (!fileBrochure.exists())
    {
        CopyAssetsbrochure();
    }

    /** PDF reader code */
    File file = new File(Environment.getExternalStorageDirectory() + "/" + "demo.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(Main2Activity.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("demo.pdf"))
        {
            InputStream in = null;
            OutputStream out = null;
            try
            {
                in = assetManager.open(files[i]);
                out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/" + files[i]);
                copyFile(in, out);
                in.close();
                in = null;
                out.flush();
                out.close();
                out = null;
                break;
            }
            catch(Exception e)
            {
                Log.e("tag", e.getMessage());
            }
        }
    }
}
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);
    }
}

}

2 个答案:

答案 0 :(得分:0)

请在try和catch中开始活动

try {
    startActivity(intent);
} 
catch (ActivityNotFoundException e) {
    Toast.makeText(this, "No Application Available to View PDF", 
        Toast.LENGTH_SHORT).show();
}

如果没有任何应用程序发现时间在App中使用Webview查看pdf文件

答案 1 :(得分:0)

删除原始文件夹和create asset folder,然后将pdf文件复制到其中。 enter image description here

现在它工作正常。试试这个。

相关问题