Can not find file directory

0

I have a method that is part of a class to capture the client signature.

But it is giving problems when opening FileOutputStream , "No such file or directory" appears

follow the method:

/**
 * 
 * @param MEDIA_DIRECTORY ex: /storage/emulated/0/appname/assinaturas-consultas/
 * @param STOREDPATH ex: /storage/emulated/0/appname/assinaturas-consultas/teste.png
 * @return
 */
public boolean save(String MEDIA_DIRECTORY, String STOREDPATH) {
    view.setDrawingCacheEnabled(true);
    if (bitmap == null)
        bitmap = Bitmap.createBitmap(640, 480, Bitmap.Config.RGB_565);

    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);
    try {
        File storageDir = new File(MEDIA_DIRECTORY);
        if (!storageDir.exists())
            storageDir.mkdirs();

        FileOutputStream out = new FileOutputStream(STOREDPATH); //Erro acontece aqui.
        view.draw(canvas);
        bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);

        out.flush();
        out.close();
        return true;
    } catch (Exception e) {
        Log.e("log_tag", e.toString());
    }
    return false;
}

I've also put these two lines in Manifest :

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
asked by anonymous 14.05.2017 / 08:24

1 answer

0

This happens because the file corresponding to STOREDPATH does not exist. You need to create the file before using it in FileOutputStream.

It would look like this:

File storedFile = new File(STOREDPATH);
if (!storedFile.exists())
  storedFile.createNewFile();

FileOutputStream out = new FileOutputStream(storedFile); //Erro acontece aqui.
    
14.05.2017 / 19:47