4

This could probably be a fast fix but currently I am unable to get this working...

I have an asynctask where I am parsing a XML file. If I place an XML file in the assets folder i can open and parse it no problem.

But if I try to open a XML file from external storage it fails.

Here is my asynctask:

private class async extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
    while (!isCancelled()) {

     try {
         NodeList nList;
         Node node;

         InputStream is = getAssets().open("file.xml");
         // this works

         File is = new File("/storage/emulated/0/test.xml");
         // this fails

         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
         DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
         Document doc = dBuilder.parse(is);

I am getting this error:

I/System.out: File path: /storage/emulated/0/test.xml
W/System.err: java.io.FileNotFoundException: /storage/emulated/0/test.xml (Permission denied)

These permissions are in my manifest:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Could someone tell me why I am getting this error?

Thanks!

2
  • 2
    NEVER HARDCODE PATHS. Your hardcoded value will be wrong on hundreds of millions of devices. Beyond that, you may not have the permission yet, if your targetSdkVersion is 23+ and you are running on Android 6.0+. Commented Dec 2, 2016 at 16:17
  • The path will not be hardcoded, this was just a test. The real absolute path will get passed through String params Commented Dec 2, 2016 at 16:44

4 Answers 4

1

I see your error message :

java.io.FileNotFoundException: /storage/emulated/0/test.xml (Permission denied)

Remember that running on running Android 6.0 you must implement runtime permissions before you try to read or write the external storage.

setting this into your manifest.xml is not enough for devices with Android 6.0+:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Sign up to request clarification or add additional context in comments.

2 Comments

Ahh I totally forgot about that! You were right! Thanks :)
Got it working now. Turns out I don't even need READ_EXTERNAL_STORAGE. WRITE_EXTERNAL_STORAGE will suffice if I grant it at runtime.
1

You should not hardcode the path. You can try this:

String path = Environment.getExternalStorageDirectory() + File.separator + MY_DIR_NAME + File.separator + "file.xml"

1 Comment

The path will not be hardcoded, this was just a test. The real absolute path will get passed through String params
1

this is the method I use to open a pdf file from the folder that I created in the internal storage (sd card) of my phone. but first you need to asd the user for the permission , go to manifest and write down :

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

and in your main activity, you must implement runtime permissions before you try to read or write the external storage.

than use this method below don't forget to change the folder and file name

public void openPDF2(){
    String path = Environment.getExternalStorageDirectory() + File.separator + "PDF folder 12"+ File.separator ;
    File file = new File(path,fileName+".pdf");

    String extension = MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());

    String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);

    Intent intent = new Intent(Intent.ACTION_VIEW);

    intent.setFlags(FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_NEW_TASK);

    Uri uri = FileProvider.getUriForFile(GenerateQRActivity.this, GenerateQRActivity.this.getApplicationContext().getPackageName() + ".provider", file);

    try {
        intent.setDataAndType(uri, mimeType);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);


        startActivity(Intent.createChooser(intent, "choseFile"));
    } catch (Exception e) {
        e.printStackTrace();
        Log.d(TAG2, "openPDF2: the problem is : "+e.getMessage());
    }
}

Comments

0

You should not try to access the filesystem using absolute paths.

To retrieve the path of the SD card you can use:

Environment.getExternalStorageDirectory()

So if you want to create a file named test.xml

new File(Environment.getExternalStorageDirectory(),"test.xml");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.