Error with FileOutputStream


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;

        Log.v("IMAGEN","true todo");
    }else if(estado.equals(Environment.MEDIA_MOUNTED_READ_ONLY)){

    if(sdDisponible && sdAccesoEscritura) {
        String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + NameOfFolder;
        String CurrentDateAndTime = getCurrentDateAndTime();
        File dir = new File(file_path);

        if (!dir.exists()) {
            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");
            Log.v("IMAGEN", "Pasa el output3");
            Log.v("IMAGEN", "Pasa el output4");
            Log.v("IMAGEN", "Pasa el output5");
        } catch (FileNotFoundException e) {
            Log.v("IMAGEN", e.toString());
        } catch (IOException e) {
            Log.v("IMAGEN", e.toString());


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: /storage/emulated/0Demo/IMG-2017-08-
    13.jpg: open failed: ENOENT (No such file or directory)
asked by Adrián Garrido Blázquez 29.08.2017 в 14:30

2 answers


If you use mkdirs () to create the directory:


and you say that you are always creating it, in fact you are trying to create it if it does not exist, and The problem here is that you probably do not have the necessary permissions, remember that the permission WRITE_EXTERNAL_STORAGE is essential.

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

in fact when you try to save the file, save it in the instance File , this file does not exist,

 FileOutputStream fOut = new FileOutputStream(file);

since it could not be created, for this reason you have the error message:


open failed: ENOENT (No such file or directory)

Another reason why you can not create the file is undoubtedly where you are generating it, in this case you use


therefore it would be created within:


It is important to add an "/" at the end to correctly create the path of the folder, otherwise this can also be a problem, this would be the solution.

String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + NameOfFolder;

As an extra data, you can validate the creation of the directory in this way, since mkdirs () delivery true if the directory was created without problem, in any other case false :

  if (!dir.exists()) {
            String message = dir.mkdirs()?"Se creo carpeta correctamente!": "No se pudo crear =(";
            Log.v("IMAGEN","crea carpeta " + message);

If you use Android 6.0 or later, the permission request must be manual, since it is not enough to declare it within the file AndroidManifest.xml , you can call this method in onCreate() to get the permissions :

private void revisaPermisosAlmacenamiento() {
    int permissionCheck = ContextCompat.checkSelfPermission(
            this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        Log.i("Mensaje", "No se tiene permiso para leer.");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 225);
    } else {
        Log.i("Mensaje", "Se tiene permiso para leer!");

check this question:

Error when displaying the external file directory in an AlertDialog in android 6.0 (READ_EXTERNAL_STORAGE / WRITE_EXTERNAL_STORAGE)

answered by 29.08.2017 / 17:04

Because you do not try to create the file with the openFileOutput method:

 context.openFileOutput(FileName, Context.MODE_PRIVATE);

Look in the android documentation: link

answered by 29.08.2017 в 16:16