I'm trying to save an image of ImageView
in the gallery of my mobile device and the problem is that I'm always creating the directory where I want to save the image and then the path when I'm going to do the% % co tells me that it does not exist. I attach the code where I am giving the ruling:
public void SaveImage(Context context, Bitmap ImageToSave) {
TheThis = context;
NameOfFolder = context.getString(R.string.app_name);
String estado=Environment.getExternalStorageState();
Boolean sdDisponible=false;
Boolean sdAccesoEscritura=false;
if(estado.equals(Environment.MEDIA_MOUNTED)){
sdDisponible=true;
sdAccesoEscritura=true;
Log.v("IMAGEN","true todo");
}else if(estado.equals(Environment.MEDIA_MOUNTED_READ_ONLY)){
sdDisponible=true;
sdAccesoEscritura=false;
}else{
sdAccesoEscritura=false;
sdDisponible=false;
}
if(sdDisponible && sdAccesoEscritura) {
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + NameOfFolder;
String CurrentDateAndTime = getCurrentDateAndTime();
File dir = new File(file_path);
if (!dir.exists()) {
dir.mkdirs();
Log.v("IMAGEN","crea carpeta");
}
File file = new File(dir, NameOfFile + CurrentDateAndTime + ".jpg");
Log.v("IMAGEN", "Pasa el file");
try {
FileOutputStream fOut = new FileOutputStream(file);
Log.v("IMAGEN", "Pasa el output");
ImageToSave.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
Log.v("IMAGEN", "Pasa el output2");
fOut.flush();
Log.v("IMAGEN", "Pasa el output3");
fOut.close();
Log.v("IMAGEN", "Pasa el output4");
MediaStore.Images.Media.insertImage(context.getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
MakeSureFileWasCreatedThenMakeAvabile(file);
Log.v("IMAGEN", "Pasa el output5");
AbleToSave();
} catch (FileNotFoundException e) {
UnableToSave();
Log.v("IMAGEN", e.toString());
} catch (IOException e) {
UnableToSave();
Log.v("IMAGEN", e.toString());
}
}else{
UnableToSave();
}
}
I pass the console with the FileOutputStream
of where the code is executed:
08-29 14:28:13.374 8582-8582/es.aandg.demo V/IMAGEN: true todo
08-29 14:28:13.386 8582-8582/es.aandg.demo V/IMAGEN: crea carpeta
08-29 14:28:13.386 8582-8582/es.aandg.demo V/IMAGEN: Pasa el file
08-29 14:28:13.409 8582-8582/es.aandg.demo V/IMAGEN:
java.io.FileNotFoundException: /storage/emulated/0Demo/IMG-2017-08-
29_14-28-
13.jpg: open failed: ENOENT (No such file or directory)