0

I'm trying to make a really simple app which allow users write on the image provided and save it to their gallery. And I tried the code below. And it trigger error and cannot save the image.

@Override
public boolean onOptionsItemSelected(MenuItem item) {


switch(item.getItemId()){
    case R.id.savebutton:
             Bitmap bitmap = ((BitmapDrawable)imagecp.getDrawable()).getBitmap();


            saveBitmap(bitmap);

    default:
        return super.onOptionsItemSelected(item);


}

public void saveBitmap(Bitmap bmp) {
    String _time = "";
    Calendar cal = Calendar.getInstance();
    int millisecond = cal.get(Calendar.MILLISECOND);
    int second = cal.get(Calendar.SECOND);
    int minute = cal.get(Calendar.MINUTE);
    int hourofday = cal.get(Calendar.HOUR_OF_DAY);
    _time = "image_" + hourofday + "" + minute + "" + second + ""
            + millisecond + ".png";
    String file_path = Environment.getExternalStorageDirectory()
            .getAbsolutePath();
    try {
        File dir = new File(file_path);
        if (!dir.exists())
            dir.mkdirs();
        File file = new File(dir, _time);
        FileOutputStream fOut = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, fOut);
        fOut.flush();
        fOut.close();
        Toast.makeText(getApplicationContext(),
                "Image has been saved in Couponmaker folder",
                Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(),
                "saving failed",
                Toast.LENGTH_LONG).show();
        Log.e("error in saving image", e.getMessage());
    }
}

and error message

 E/error in saving image﹕ /storage/0C11-3814/image_162259256.png: open failed: EACCES (Permission denied)
W/EGL_emulation﹕ eglSurfaceAttrib not implemented
W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xad78df60, error=EGL_SUCCESS
E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xab7dd67 W/art﹕ Suspending all threads took: 13.866ms

xml file

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

<application
    android:allowBackup="true"....
JessJ
  • 57
  • 9

1 Answers1

0

I believe in Marshmallow, external device directory is the sd card directory. I was trying to write on the sd card but failed. Because they don't allow to do that (I read somewhere but I can not add the link here since I forgot). Gallery does not necessarily read the image from external storage. Look into this answer, might help and solve your problem: How to save image in android gallery

Community
  • 1
  • 1
Omar Faroque Anik
  • 2,531
  • 1
  • 29
  • 42