N
N
Nurshat2016-11-13 20:41:39
Java
Nurshat, 2016-11-13 20:41:39

How to save bitmap to file?

Hello.
I'm trying to save a bitmap to a file using this code:

Bitmap bmp1 = Bitmap.createBitmap(result.cols(), result.rows(), Bitmap.Config.ARGB_8888);

 File file = new File(context.getFilesDir(), "imageStitched.png");
        System.out.println("-------======= "+ context.getFilesDir() +" =======-------");

        try {
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(file);
                bmp1.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            } finally {
                if (fos != null) fos.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

There are no errors, and judging by sout, the file should have been saved along the path /data/user/0/com.example.nurshat.imagesstitching/files , but the /com.example.nurshat.imagesstitching folders in the /data/user/0 directory have I don't exist at all.
VMQwI4Nzalw.jpg
Help please deal with the problem.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Odissey Nemo, 2016-11-17
@odissey_nemo

I don't know how in Android, but in a normal file system, I would first check if the specified directory exists with a full path (without a file name). And if it doesn't exist, then I would create it, if there are rights. For our case it will be like this:

if ( !file.getParentFile().exists())
{
     java.nio.file.Files.createDirectory( file.getParentFile().toPath() );
}

error handling is omitted for simplicity. The question why the directory does not exist requires a separate study.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question