I'm following a tutorial online to make a simple drawing application. Everything works fine up to the part where the drawing is saved to the internal storage. For some reason, the image name is always null even when there is clearly a name assigned to it. I'm running the application on a Axon 7 Mini running Android Nougat. Here is the code that I cannot get to work. I have the proper permission in the Manifest. And does it matter if you save the image as a JPEG vs a PNG?
Picture can't be saved into phone storage
else if(view.getId()==R.id.save_btn){
//save drawing
AlertDialog.Builder saveDialog = new AlertDialog.Builder(this);
saveDialog.setTitle("Save drawing");
saveDialog.setMessage("Save drawing to device Gallery?");
saveDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
//save drawing
drawView.setDrawingCacheEnabled(true);
//attempt to save
String imgSaved = null;
OutputStream fOut = null;
File mediaDirectory = new File(Environment.getExternalStorageDirectory().getPath()
+ "DCIM/Camera/");
mediaDirectory.mkdirs();
File f = new File(mediaDirectory, UUID.randomUUID().toString() + ".png");
try {
fOut = new FileOutputStream(f);
drawView.getDrawingCache().compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
imgSaved = "success";
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse
("file://" + Environment.getExternalStorageDirectory())));
}
catch (Exception e) {
e.printStackTrace();
}
//feedback
if(imgSaved!=null){
Toast savedToast = Toast.makeText(getApplicationContext(),
"Drawing saved to Gallery!", Toast.LENGTH_SHORT);
savedToast.show();
}
else{
Toast unsavedToast = Toast.makeText(getApplicationContext(),
"Oops! Image could not be saved.", Toast.LENGTH_SHORT);
unsavedToast.show();
}
drawView.destroyDrawingCache();
}
});
saveDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
dialog.cancel();
}
});
saveDialog.show();
}