Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a pdf stored either in res/raw or assets folder?

I'm going to show a pdf in my application, and the pdf has to be bundled with the application.

What is a good way to do this?

I have read that it might be possible to do this by adding the pdf file to a res/raw folder and read it from there, but i get project errors when i put the pdf file there.

So i tried to put the pdf file in the asset folder of the project, and it gave no errors.

This is how i've tried to show the pdf:

File pdfFile = new File("res/raw/file.pdf");
Uri path = Uri.fromFile(pdfFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Any ideas or suggestions?

Thanks in advance

like image 816
Ikky Avatar asked Jun 27 '11 09:06

Ikky


People also ask

What is a raw folder?

The raw (res/raw) folder is one of the most important folders and it plays a very important role during the development of android projects in android studio. The raw folder in Android is used to keep mp3, mp4, sfb files, etc. The raw folder is created inside the res folder: main/res/raw.


4 Answers

You cannot open the pdf file directly from the assets folder.You first have to write the file to sd card from assets folder and then read it from sd card.The code is as follows:-

     @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    File fileBrochure = new File(Environment.getExternalStorageDirectory() + "/" + "abc.pdf");
    if (!fileBrochure.exists())
    {
         CopyAssetsbrochure();
    } 

    /** PDF reader code */
    File file = new File(Environment.getExternalStorageDirectory() + "/" + "abc.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(SecondActivity.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("abc.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);
        }
    }

Thats all..Enjoy!! and please dont forget to give +1.Thanks

like image 164
Deepak Sharma Avatar answered Oct 23 '22 14:10

Deepak Sharma


You would be able to show it from raw/ or assets/ if your application actually implemented a PDF reader. Since you want it to be displayed in a separate application (such as Adobe Reader), I suggest doing the following:

  1. Store the PDF file in the assets/ directory.
  2. When the user wants to view it, copy it somewhere public. Look into openFileOutput or getExternalFilesDir.
  3. Launch the Intent just like you are doing now, except use getAbsolutePath() on the newly created file for the intent's data.

Be aware that a user might not have a PDF reading application. In this case, it is useful to catch the ActivityNotFoundException and show an appropriate message.

like image 29
Felix Avatar answered Oct 23 '22 12:10

Felix


I've used the following format to open raw resources from within my own application. I haven't tested whether another application can open your raw resource.

Uri path = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.myPdfName);
like image 34
David Snabel-Caunt Avatar answered Oct 23 '22 13:10

David Snabel-Caunt


You pdf intent seems good but you should try this to get the Uri of file in the raw folder :

Uri path = Uri.parse("android.resource://<you package>/raw/<you file.pdf>");

(Source)

like image 35
Snicolas Avatar answered Oct 23 '22 12:10

Snicolas