Permission denied when reading a file

0

What a good day community. I am new to the group and I need your help.

I have a java code to read a text file by line and store it in a matrix to return the value of the matrix.

I compile the code in netbeans and it returns the value to me correctly. by adapting this same code in Android studio, I returned a permission error.

The SD read permissions were granted in the file Manifest file and I presented the following error (taken from Logcat):

  

I / System.out: /storage/emulated/0/Download/script.txt W / System.err:   java.io.FileNotFoundException: /storage/emulated/0/Download/script.txt   (Permission denied) W / System.err: at   java.io.FileInputStream.open0 (Native Method) W / System.err: at   java.io.FileInputStream.open (FileInputStream.java:200) W / System.err:
  at java.io.FileInputStream. (FileInputStream.java:150)   W / System.err: at   java.io.FileInputStream. (FileInputStream.java:103) W / System.err:   at java.io.FileReader. (FileReader.java:58) W / System.err: at   com.lexuntu.cda.launcherbox.LeerArchivo.LeerArchivo (LeerArchivo.java:18)   W / System.err: at   com.lexuntu.cda.launcherbox.MainActivity.onActivityResult (MainActivity.java:34)   W / System.err: at   android.app.Activity.dispatchActivityResult (Activity.java:7276)   W / System.err: at   android.app.ActivityThread.deliverResults (ActivityThread.java:4264)   W / System.err: at   android.app.ActivityThread.handleSendResult (ActivityThread.java:4312)   W / System.err: at android.app.ActivityThread.-wrap19 (Unknown   Source: 0) W / System.err: at   android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1644)   W / System.err: at   android.os.Handler.dispatchMessage (Handler.java:106) W / System.err:
  at android.os.Looper.loop (Looper.java:164) W / System.err: at   android.app.ActivityThread.main (ActivityThread.java:6494)   W / System.err: at java.lang.reflect.Method.invoke (Native Method)   W / System.err: at   com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run (RuntimeInit.java:438)   W / System.err: at   com.android.internal.os.ZygoteInit.main (ZygoteInit.java:807)   D / EGL_emulation: eglMakeCurrent: 0xe5305120: see 2 0 (tinfo   0xe5303340)

Annex fragment of code, I hope you serve them and can support me to solve this problem.

//CLASE LeerArchivo
public class LeerArchivo {

    public static String [] LeerArchivo(String Archivo) throws IOException {

        File file = new File(Archivo);
        FileReader Fr = new FileReader(Archivo);
        BufferedReader Br = new BufferedReader(Fr);

       String matriz[]=new String [39];

        int cont=0;
        try {
            String lines = "";
            while( ( lines = Br.readLine()) != null) {
                matriz[cont]=lines;
                cont++;
            }
        } catch (FileNotFoundException e) {
            System.out.println("No se encontro el Archivo "+file.getName());
        }
        Fr.close();
        Br.close();
        return matriz;
    }
}

///fragmento de MainActivity


    protected void onActivityResult(int requestCode,int resultCode,Intent data) {
        super.onActivityResult(requestCode,resultCode,data);
        if (resultCode == RESULT_CANCELED) {   //Cancelado por el usuario
        }
        if ((resultCode == RESULT_OK) && (requestCode == VALOR_RETORNO)) {//Procesar el resultado
            Uri uri = data.getData();//getData(); //obtener el uri content
            arch = Uri.parse(String.valueOf(uri)).getLastPathSegment().substring(4);
            System.out.println(arch);
            try {
                String[] Times = new String[39];
                Times = LeerArchivo.LeerArchivo(arch);
                //llenarTimes(arch);
              for(int i=0;i<=39;i++){
                  System.out.println(Times[i]);
              }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
asked by lexuntu 23.05.2018 в 22:58
source

1 answer

1

You do not have permission to read in the external storage directory:

  

/storage/emulated/0/Download/script.txt (Permission denied)

this may be because you have not defined the permission

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

Remember if you use Andriod operating system 6.0 or higher you must manually require permissions:

Error showing the external file directory in an AlertDialog in android 6.0 (READ_EXTERNAL_STORAGE / WRITE_EXTERNAL_STORAGE)

    
answered by 23.05.2018 в 23:30