Save image coming from API in mobile memory

0

I'm trying to save an image coming from the API into the phone's memory.

    public void saveSkin() {
    ivSkinSaver.buildDrawingCache();
    Bitmap bm = ivSkinSaver.getDrawingCache();

    OutputStream Out = null;
    try {
        File mediaFile;
        File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
                + "/Android/data"
                + getApplicationContext().getPackageName()
                + "/Files");
        String timeStamp = new SimpleDateFormat("dd/MM/yyyy_HH:mm_").format(new Date());
        String mImageName = timeStamp + stringSkin;
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);
        mediaFile.createNewFile();
        Out = new FileOutputStream(mediaFile);
        Toast.makeText(getBaseContext(),"file saved",Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        Toast.makeText(this, "Error occured. Please try again later." + e.getStackTrace(),
                Toast.LENGTH_SHORT).show();

        e.printStackTrace();
    }

    try {
        bm.compress(Bitmap.CompressFormat.PNG, 100, Out);
        Out.flush();
        Out.close();
    } catch (Exception e) {
    }
}
}

I used e.printStackTrace() and the result is: "java.io.IOException: open failed: EACCES (Permission denied)"

    
asked by anonymous 17.03.2017 / 16:39

1 answer

3

This may be occurring because you somehow did not grant write permission to the device. See how you should AndroidManifest.xml :

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

In addition, you should also note that if your app is running on Android 6.0+, you must grant permission at runtime. Here's how to requesting run-time permissions in the documentation itself.

From Android Marshmallow, API 23, users grant permissions to applications while they are running, not when they are installed.

  

This approach streamlines the application installation process as the   user does not need to grant permissions when installing or updating the   application.

In addition to this issue, it also gives the user more control over the features of the application. For example, a user could choose to allow a camera application to have access to the camera, but not to the location of the device. The user can revoke the permissions at any time in the application settings screen.

See an example:

if (ContextCompat.checkSelfPermission(context,WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

      // Verifica se já mostramos o alerta e o usuário negou na 1ª vez.
      if (ActivityCompat.shouldShowRequestPermissionRationale(context,Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
           // caso o usuário tenha negado a permissão anteriormente, e não tenha marcado o check "nunca mais mostre este alerta"

      } else {
          // solicita permissão
          ActivityCompat.requestPermissions(context,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},0);
      }
} else {
     // caiu aqui está tudo ok
}
    
17.03.2017 / 16:48