7

I want to open a .pdf file in my android app.now i can browse the pdf file and after browsing the file I am getting File Not Found Error when i check the file exist or not. Now after selecting the file my selected file Uri data.getData() is like

content://com.android.externalstorage.documents/document/6333-6131:SHIDHIN.pdf

and the path when i parse using data.getData().getPath().toString() is like

/document/6333-6131:SHIDHIN.pdf Here is my code. Please Help me.

// To Browse the file

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/pdf");
startActivityForResult(intent, PICK_FILE_REQUEST);

After selecting file

//onActivityResult

public void onActivityResult(final int requestCode, int resultCode, Intent data) {
    try {
        switch (requestCode) {
            case PICK_FILE_REQUEST:
                if (resultCode == RESULT_OK) {
                    try {
                        Uri fileUri = data.getData();
                        String path  = fileUri.getPath().toString();
                        File f = new File(path);
                        if (f.exists()) {
                            System.out.println("\n**** Uri :> "+fileUri.toString());
                            System.out.println("\n**** Path :> "+path.toString());
                            final Intent intent = new Intent(MainActivity.this, ViewPdf.class);
                            intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, path);
                            startActivity(intent);
                        } else {
                            System.out.println("\n**** File Not Exist :> "+path);
                        }

                    } catch (Exception e) {
                        ShowDialog_Ok("Error", "Cannot Open File");
                    }
                }
                break;
        }
    } catch (Exception e) {
    }
}
Froyo
  • 17,947
  • 8
  • 45
  • 73
SHIDHIN TS
  • 1,557
  • 3
  • 26
  • 58
  • If the file is stored in an app's internal cache, android does not allow other apps to access it. So maybe that could be the problem. – Froyo May 06 '15 at 12:09
  • The file is stored in the external directiory. – SHIDHIN TS May 06 '15 at 12:12
  • You can not just convert uri to absolute path. you need to use `ContentResolver`. – Froyo May 06 '15 at 12:15
  • Check this answer. http://stackoverflow.com/a/16511111/891373 – Froyo May 06 '15 at 12:17
  • @Froyo that is also not working for me. I am getting an null pointer exception "ava.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference" – SHIDHIN TS May 06 '15 at 12:26
  • Also, is it necessary to pass absolute file path? Could you pass Uri only? and use it as `context.getContentResolver().openInputStream(uri)` ? – Froyo May 06 '15 at 12:37
  • Can u show me one Example using context.getContentResolver().openInputStream(uri). – SHIDHIN TS May 06 '15 at 12:52

2 Answers2

4

This is not the answer but a workaround.

File file = new File("some_temp_path"); # you can also use app's internal cache to store the file
FileOutputStream fos = new FileOutputStream(file);

InputStream is = context.getContentResolver().openInputStream(uri);
byte[] buffer = new byte[1024];
int len = 0;
try {
    len = is.read(buffer);
    while (len != -1) {
        fos.write(buffer, 0, len);
        len = is.read(buffer);
    }

    fos.close();
} catch (IOException e) {
    e.printStackTrace();
}

pass this file's absolute path to your activity.

Froyo
  • 17,947
  • 8
  • 45
  • 73
  • This is also not working. we are getting Nullpointer error. I think because of the problem with url. when we retrieve the path from the url the file not exist error is getting. – SHIDHIN TS May 06 '15 at 13:18
  • Where are you getting the error? Update the question with logs – Froyo May 06 '15 at 13:19
  • Hay Sorry.. That was my Mistake I didn't change the some_temp_path. Now its working fine. Thanks Thanks a lot. – SHIDHIN TS May 06 '15 at 13:24
0
button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        File pdfFile = new File(Environment.getExternalStorageDirectory(), "Case Study.pdf");

        try {
            if (pdfFile.exists()) {
                Uri path = Uri.fromFile(pdfFile);
                Intent objIntent = new Intent(Intent.ACTION_VIEW);
                objIntent.setDataAndType(path, "application/pdf");
                objIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(objIntent);
            } else {
                Toast.makeText(MainActivity.this, "File NotFound", Toast.LENGTH_SHORT).show();
            }
        } catch (ActivityNotFoundException e) {
            Toast.makeText(MainActivity.this, "No Viewer Application Found", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});
Ziem
  • 6,579
  • 8
  • 53
  • 86
Ed.
  • 304
  • 2
  • 16