Home > front end >  java.nio.file.AccessDeniedException with android studio when creating an app but no issues with only
java.nio.file.AccessDeniedException with android studio when creating an app but no issues with only

Time:05-24

This code works with no issue in java reading the file in my laptop, but when I am trying to create the app with Android studio, v 9.0, so far the app cannot read this file located in my phone.

String thePath = String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)); 

Path fileLocation = Paths.get( thePath "/v.txt");

so fileLocation is being passed as parameter to this method:

public void mainOperation(Path fileLocation) throws IOException {
 if (!Files.exists(fileLocation)) {
     System.out.println("File Doesn’t exist. Exiting…");
     System.exit(1);
 }
    StringBuilder mysb = new StringBuilder();
    // problem is when trying to read the file, in the below line
    for (String str : Files.readAllLines(fileLocation, Charset.defaultCharset())) {
        System.out.println("reading....");
        mysb.append(str);
        mysb.append(";");

        if (str.contains(END_VCARD)) {

            System.out.println(startProcess(mysb.toString()));
            mysb = new StringBuilder();
        }
    }
}

when I debug the app, I see that the error is coming from here in MainActivity.java:

binding.fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        ProcessVcard myprocessVcard = new ProcessVcard();
        String thePath = 

String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS))

Path fileLocation = Paths.get( thePath   "/v.txt");
        try {
            myprocessVcard.mainOperation(fileLocation);
        } catch (IOException e) {
            e.getCause(); // 
            e.printStackTrace();
        }


   

and IOException is caught with the following error: java.nio.file.AccessDeniedException: /storage/emulated/0/Download/v.txt

I did add uses-permission in AndroidManifest.xml file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.app4">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application

I also tried the option of clean project but so far no joy.

v.txt can be opened in my mobile. Also, please see the view from Device File Explore in Android Studio: enter image description here

Any advice?

Many thanks

CodePudding user response:

On Android 11 , you do not have access to files placed in Downloads/ by other apps, when using filesystem APIs or MediaStore.

Instead, you could use ACTION_OPEN_DOCUMENT / ActivityResultContracts.OpenDocument. That will let the user place the file wherever the user wants.

CodePudding user response:

I am very happy to say that this simple app is working now and doing the job, not bad for the fist one !!

I reviewed again all the setting in my mobile and in setting / apps / my app, I see that in permission there is an option now to select storage. I think this probably came after adding the uses-permission in AndroidManifest xml file, otherwise cannot say how this setting is available.

Thanks @CommonsWare for your help anyway.

  • Related