Can’t find file directory

Asked

Viewed 37 times

0

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

But he’s giving trouble when it comes to opening the FileOutputStream, appears "No such file or directory"

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 on Manifest:

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

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.

Would look like this:

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

FileOutputStream out = new FileOutputStream(storedFile); //Erro acontece aqui.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.