2

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!

Surya Prakash Kushawah
  • 3,185
  • 1
  • 22
  • 42
Bavilo
  • 389
  • 1
  • 6
  • 19
  • 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](https://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it), if your `targetSdkVersion` is 23+ and you are running on Android 6.0+. – CommonsWare Dec 02 '16 at 16:17
  • The path will not be hardcoded, this was just a test. The real absolute path will get passed through String params – Bavilo Dec 02 '16 at 16:44

4 Answers4

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" />
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • Ahh I totally forgot about that! You were right! Thanks :) – Bavilo Dec 02 '16 at 16:31
  • 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. – Bavilo Dec 02 '16 at 16:45
1

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

String path = Environment.getExternalStorageDirectory() + File.separator + MY_DIR_NAME + File.separator + "file.xml"
FerDensetsu
  • 736
  • 5
  • 20
  • The path will not be hardcoded, this was just a test. The real absolute path will get passed through String params – Bavilo Dec 02 '16 at 16:42
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");
Nissa
  • 4,636
  • 8
  • 29
  • 37
Arthur
  • 390
  • 1
  • 4
  • 13
0

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());
    }
}
jenos kon
  • 504
  • 4
  • 7