开发者

Open pdf files located inside the App (raw folder)

开发者 https://www.devze.com 2023-03-22 14:39 出处:网络
I need to open in an external app (like Adobe Reader) some pdf fi开发者_运维技巧les located inside the app (raw folder)

I need to open in an external app (like Adobe Reader) some pdf fi开发者_运维技巧les 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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消