On some devices following code does not save the bitmap to the gallery. I was told this is a Samsung device. Is there any improvements I can make to resolve this issue.
protected Void doInBackground(Bitmap... data) {
try {
if (data.length > 0 && data[0] == null) return null;
ContentResolver resolver = mContext.getContentResolver();
String fileName = "XF_" + new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss").format(new Date()) + ".jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + File.separator + mContext.getString(R.string.app_name));
values.put(MediaStore.MediaColumns.IS_PENDING, 1);
} else {
File directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + Environment.DIRECTORY_PICTURES);
if(!directory.exists()){
directory.mkdirs();
}
File file = new File(directory, fileName);
values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
}
Uri uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
OutputStream output = resolver.openOutputStream(uri);
data[0].compress(Bitmap.CompressFormat.JPEG, 100, output);
output.flush();
output.close();
Log.d("myApp", "Image saved!");
} catch (IOException e) {
Log.d("myApp", "Image saved!" + e.getMessage());
e.printStackTrace();
} catch (Exception e) {
Log.d("myApp", "Image saved!" + e.getMessage());
e.printStackTrace();
}
return null;
}