1

I want to open a PDF file in my application but something goes wrong everytime. Yeah , I watched many topics about it, however none of them helped me.

Here are some photos of the error:

https://i.stack.imgur.com/TZpO2.jpg

https://i.stack.imgur.com/CuoeM.jpg

 btn3.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    //startActivity(new Intent(MainActivity.this, Activity2.class));
                                    File file = null;
                                    file = new File(Environment.getExternalStorageDirectory() + "/raw/" + "tirepressuremonitoringsystem3.pdf");
                                    Toast.makeText(getApplicationContext(), file.toString() , Toast.LENGTH_LONG).show();
                                    if(file.exists()) {
                                        Intent target = new Intent(Intent.ACTION_VIEW);
                                        target.setDataAndType(Uri.fromFile(file), "application/pdf");
                                        target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

                                        Intent intent = Intent.createChooser(target, "Open File");
                                        try {
                                            startActivity(intent);
                                        } catch (ActivityNotFoundException e) {
                                            // Instruct the user to install a PDF reader here, or something
                                        }
                                    }
                                    else
                                        Toast.makeText(getApplicationContext(), "File path is incorrect." , Toast.LENGTH_LONG).show();
                                }
                            }
    );

Maybe the error is that I put these files in /raw ? Should I store them in assest folder instead?

Hans1984
  • 796
  • 11
  • 24
gdd
  • 32
  • 1
  • 1
  • 7

3 Answers3

1

It seems you want to access file from raw folder @gdd.

Since while compiling your android project R.java file is get genrated , you can access whatever in your raw folder by R.raw.fileName and in your case you want to access the file name tirepressuremonitoringsystem3.pdf, So You can use R.raw.tirepressuremonitoringsystem3.

If You dont wnat to go this way and want to use jdks file api .. this question will help you Thanks and keep coding.

Community
  • 1
  • 1
Mitesh Ukate
  • 173
  • 2
  • 14
0

Replace your this line

 file = new File(Environment.getExternalStorageDirectory() + "/raw/" + "tirepressuremonitoringsystem3.pdf");

with this line

 file = new File("android.resource://com.cpt.sample/raw/tirepressuremonitoringsystem3.pdf");
Ravi Makvana
  • 2,872
  • 2
  • 24
  • 38
  • http://i.imgur.com/jKrwPKh.jpg now i get this – gdd Jun 15 '15 at 08:04
  • Replace com.cpt.sample with your package name – Ravi Makvana Jun 15 '15 at 08:07
  • if u can't give static package name then place this line File file = new File("android.resource://+" + getPackageName() +"/raw/tirepressuremonitoringsystem3.pdf"); – Ravi Makvana Jun 15 '15 at 08:09
  • i changed into full path : file = new File("android.resource://com.example.dovydas.myapplication3/res/raw/tirepressuremonitoringsystem3.pdf"); still path is bad – gdd Jun 15 '15 at 08:13
  • com.example.dovydas.myapplication3 is this AndroidManifest.xml file package name? – Ravi Makvana Jun 15 '15 at 08:15
  • File file = new File("android.resource://+" + getPackageName() +"/raw/tirepressuremonitoringsystem3.pdf"); just place this line and try because i think so com.example.dovydas.myapplication3 this is not a package name – Ravi Makvana Jun 15 '15 at 08:16
  • i think so.. http://i.imgur.com/DdOV4Xz.jpg – gdd Jun 15 '15 at 08:19
  • are you try File file = new File("android.resource://+" + getPackageName() +"/raw/tirepressuremonitoringsystem3.pdf"); this code?? – Ravi Makvana Jun 15 '15 at 08:22
  • Yes i did, the path is wrong – gdd Jun 15 '15 at 08:23
  • move your pdf from row to Assets folder and change path from raw to Assets and try this one definitely work – Ravi Makvana Jun 15 '15 at 08:32
  • Would be worth trying to create the uri like so: `Uri.parse("android.resource://com.example.dovydas.myapplication3/res/raw/tirepressure‌​monitoringsystem3.pdf")` and pass that to the intent. Rather than going through a file object. No idea if it will work though... – weston Jun 15 '15 at 08:42
  • YEAH , it works thanks a lot – gdd Jun 15 '15 at 08:44
0

Use raw if you want to access it by a identifier at runtime:

R.raw.tirepressuremonitoringsystem3

Use assets if you want to access it by string name.

Either way you will need to copy it out of your app to a location another app has access to, as detailed in these answers:

Community
  • 1
  • 1
weston
  • 54,145
  • 21
  • 145
  • 203