0

Hello I am using below code to open .pdf file from raw folder which is inside res folder.

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);

It gives me below error :

 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 }

What should be the issue ? Tell me if there any other option available to read pdf file from raw folder NOTE : I have go through solutions provided on stackoverflow but, no success. Thanks.

EDIT:

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);
    }
}

}

Mashuk Khan
  • 67
  • 11

2 Answers2

0

Please start activity within the try and catch

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

If no any application found that time View pdf file using Webview within a App

Sagar Vasoya
  • 158
  • 1
  • 12
  • its just handling exception. this common thing i know bro.. tell the solution to read the file sucessfully. Error handling i know..:) – Mashuk Khan Sep 15 '17 at 05:27
  • 1
    You have to store file from raw to sdcard and after display, follow this answer https://stackoverflow.com/questions/6491210/how-to-open-a-pdf-stored-either-in-res-raw-or-assets-folder – Sagar Vasoya Sep 15 '17 at 05:31
0

Remove your raw folder and create asset folder and then copy your pdf file inside it. enter image description here

Now it's working fine. Try this.

Ankita
  • 1,129
  • 1
  • 8
  • 15