PDF file path is incorrect

935 views Asked by At

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?

3

There are 3 answers

11
Ravi Makvana On BEST ANSWER

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");
0
Mitesh Ukate On

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.

2
weston On

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: