0

I'm saving image from glide to device. I'm asking for permission in first App run cause that's what my app do.

 final File myDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DesiJewellery/");
public void saveImage(Bitmap bitmap, String img_title) {

    fname = img_title;
    myDir.mkdirs();
    File image = new File(myDir, fname);


    FileOutputStream outStream;
    if (image.exists()) {
        image.delete();
    }
    try {
        outStream = new FileOutputStream(image);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, outStream);
        outStream.flush();
        outStream.close();
        success = true;
    } catch (FileNotFoundException e) {

        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }

    if (success) {
        Toast.makeText(getActivity(), "Design saved - " + img_title, Toast.LENGTH_SHORT).show();
    } else {
       // Toast.makeText(getActivity(), "Something went wrong.", Toast.LENGTH_LONG).show();
    }
    // this one to show in gallery:

}

It's working fine in emulator, but in real device it shows

java.io.FileNotFoundException: /storage/emulated/0/DesiJewellery/m_aad14.jpg (Permission denied)

Permission Check. I run it on MainActivity.

public void isStoragePermissionGranted() {

    if (Build.VERSION.SDK_INT >= 23) {
        if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED && checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {

        } else {
            requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            requestPermissions(new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
        }
    } else {

    }
}  

P.S.- I have 2 Real devices. It's working in Mashmallow and Nougat Emulator but Problem is in Only Oreo.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
mukeshsoni
  • 483
  • 8
  • 29

2 Answers2

1

You should add WRITE_EXTERNAL_PERMISSION in Android Mainifest like below :

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" 
tools:node="replace"/>
Rishav Singla
  • 485
  • 4
  • 10
1

You can try the following:

AsyncTask fileTask = new AsyncTask() {
    @Override
    protected Object doInBackground(Object[] objects) {
File directory = new File(Environment.getExternalStorageDirectory() + File.separator + "MyApplication");
if (!directory.exists()) {
                directory.mkdirs();
            }
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String name = " "+n+".jpg";
File pictureFile = new File(directory, name);
pictureFile.createNewFile();
try {
FileOutputStream out = new FileOutputStream(pictureFile);
   finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
   out.close();
} catch (Exception e) {
   e.printStackTrace();
}
return null;
 }
};
fileTask.execute();

Refer this for help

Anu Bhalla
  • 422
  • 4
  • 11