Error loading image from path with BufferedInputStream

1

Good morning, I'm trying to send an image from JAVA-ANDROID to a server via ftp, this is the code I'm using within an asynchronous task.

 try {

            FTPClient ftpClient = new FTPClient();
            ftpClient.connect(InetAddress.getByName("ftp.miweb.com.co"));
            ftpClient.login("[email protected]", "miclave");

            ftpClient.changeWorkingDirectory("/micarpeta");
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            BufferedInputStream buffIn = null;
            buffIn = new BufferedInputStream(new FileInputStream("/storage/8ABF-1303/Download (1)/soda_stereo_03.jpeg"));
            ftpClient.enterLocalPassiveMode();
            ftpClient.storeFile("foto.jpg", buffIn);

            buffIn.close();
            ftpClient.logout();
            ftpClient.disconnect();

        } catch (Exception s) {
            Log.i("consola", "Ups...");
            return "false";
        }

The problem is in this line, when trying to load the image from the Uri

buffIn = new BufferedInputStream(new FileInputStream("/storage/8ABF-1303/Download (1)/soda_stereo_03.jpeg"));

I get the following error in the LOGCAT:

java.io.FileNotFoundException: /storage/8ABF-1303/Download (1)/soda_stereo_03.jpeg: open failed: EACCES (Permission denied)

In the end it says permission denied, these are my permissions of the manifest:

<uses-feature android:name="android.hardware.camera" 
android:required="false">
</uses-feature>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

I already investigated a good time and I have not found much about it, I hope you can help me, thanks

This is the tutorial of which I was guided: link

    
asked by Alvaro Fabian M 09.02.2018 в 20:50
source

1 answer

2

Although the error message says FileNotFoundException , it's actually a permission problem:

  

java.io.FileNotFoundException: / storage / 8ABF-1303 / Download   (1) /soda_stereo_03.jpeg: open failed: EACCES (Permission denied)

You must use:

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

instead of:

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

When you require permission WRITE_EXTERNAL_STORAGE , the request for READ_EXTERNAL_STORAGE is not necessary.

Remember that for devices with Android 6.0 or later, this request must be manual, I add this method that can be helpful.

private void checkExternalStoragePermission() {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
    int permissionCheck = ContextCompat.checkSelfPermission(
            this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        Log.i("Mensaje", "No se tiene permiso para leer.");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 225);
    } else {
        Log.i("Mensaje", "Se tiene permiso para leer!");
    }
  }
}
    
answered by 09.02.2018 / 20:57
source