File not found error after selecting a file in android
Asked Answered
I

2

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) {
    }
}
Imbue answered 6/5, 2015 at 11:13 Comment(7)
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.Huonghupeh
The file is stored in the external directiory.Imbue
You can not just convert uri to absolute path. you need to use ContentResolver.Huonghupeh
Check this answer. https://mcmap.net/q/73848/-convert-file-uri-to-file-in-androidHuonghupeh
@Huonghupeh 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"Imbue
Also, is it necessary to pass absolute file path? Could you pass Uri only? and use it as context.getContentResolver().openInputStream(uri) ?Huonghupeh
Can u show me one Example using context.getContentResolver().openInputStream(uri).Imbue
H
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.

Huonghupeh answered 6/5, 2015 at 12:49 Comment(3)
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.Imbue
Where are you getting the error? Update the question with logsHuonghupeh
Hay Sorry.. That was my Mistake I didn't change the some_temp_path. Now its working fine. Thanks Thanks a lot.Imbue
H
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();
        }
    }
});
Hornet answered 6/5, 2015 at 11:19 Comment(1)
My .pdf file may change. The user can select the file from their own device.Imbue

© 2022 - 2024 — McMap. All rights reserved.