1

I need to open in an external app (like Adobe Reader) some pdf files located inside the app (raw folder)

I try to use this code that work perfect if the files are in the SD card but, if i try to put the app (package) directory in the file line, nothing happens:

public void onClick(View v) {
            File file = new File("/sdcard/myfile2.pdf");

            if (file.exists()) {
                Uri path = Uri.fromFile(file);
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(path, "application/pdf");
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

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

Also i try this code. The app Adobe reader open but i receive an invalid access directory error:

public void onClick(View v) {
            Intent i = new Intent();
            i.setAction(Intent.ACTION_VIEW);

                 i.setDataAndType(Uri.parse("file:///data/app/mypackage/res/raw/myfile.pdf"),
            "application/pdf");

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

Last solution is to copy the pdf file every time from the raw folder to the SD card, like this tutorial.

But is really complicated and a lot of code.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Gufo
  • 21
  • 1
  • 4
  • "file:///data/app/mypackage/res/raw/myfile.pdf" is not a valid URL, as there are no literal file-system files within the package. If it's possible to access the contents of an .apk via a URL, it would have to be one of a different form - all file:///data/app/... can get you is the zip file that is the .apk itself. – Chris Stratton Jul 21 '11 at 19:26
  • would you be so kind as to show me onSave code, I think that the problem is with File file =new File(...); – Alex Apr 30 '13 at 09:23

2 Answers2

1

If you wrote the pdf using the app, the apps file permissions might not allow external visibility. Check out: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

Noah
  • 1,966
  • 1
  • 14
  • 29
  • The question specifies the raw folder, which is not writable by the application, so the PDF must have been pre-packaged making this answer inapplicable. – Chris Stratton Jul 21 '11 at 19:14
  • Thanks to all. I check the link above and i find this tip: "If you want to save a static file in your application at compile time, save the file in your project res/raw/ directory. You can open it with openRawResource(), passing the R.raw. resource ID. This method returns an InputStream that you can use to read the file (but you cannot write to the original file)." Somebody can help me with a sample code? – Gufo Jul 21 '11 at 19:56
  • Yea, you would save the file in /res/raw, and then use an input stream to read bytes from the file: InputStream ins = getResources().openRawResource( R.raw.my_file ); But I think you what you really want to bundle a pdf file with your app, and then use the app to view it. It way to get that to work is by using the sd card. I think that this question would be more use to you: http://stackoverflow.com/questions/6491210/how-to-open-a-pdf-stored-either-in-res-raw-or-assets-folder – Noah Jul 21 '11 at 20:20
0

try this things:

File mFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
                "your_file.ext"); // ext meens extension .doc .zip ....

then save your data to this file, make an intent, that you'v made... but use the mFile instead of creating new file or

File fileForUri=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+"your_file.ext"

try this

Alex
  • 1,416
  • 1
  • 14
  • 24