Save image from API to mobile memory

Asked

Viewed 262 times

0

I’m trying to save an image coming from the API in mobile 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 the e.printStackTrace() and the result is: "java.io.Ioexception: open failed: EACCES (Permission denied)"

  • 1

    Maybe because you don’t have permission anyway. Check your Manifest if granted write permission on the device: android.permission.WRITE_EXTERNAL_STORAGE

  • 1

    Also, if you have using Android 6.0 have to grant permission at runtime.

  • I already put the WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE but still with the same problem.

  • Which version of Android is running the application?

  • @ramaral is in version 6.0

  • @Wellingtonyogui the answer below met you or need some more information?!

Show 1 more comment

1 answer

3

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

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

Also, you should also note if the use of the application is on Android 6.0+ you should grant permission at runtime. See how to make a request for runtime 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 optimizes the application installation process, as the user does not need to grant permissions when installing or updating app.

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 app access to the camera, but not the location of the device. The user can revoke permissions at any time on the application settings screen.

Take 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
}
  • I already have a method that does this validation, but msm so falls in catch when saving.

  • As "Permission denied" appeared, the main factor to consider is the question of reading and writing permission. Maybe it’s your algorithm to save the file then. You take the image from a URL to save on the right device?

  • Yes, I get the image link by api and step to an imageView and put a button to save the image.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.