0

I am trying to make an app which can generate an image file(JPEG/PNG) with dimensions equal to the user's phone's screen size and fill it with a single solid color(BLACK/BLUE/GRAY, etc). After generating it, it should save the image to the external storage to be used in any other app. Any help would be appreciated. So far I have been able to do this based on a few answers but it doesn't generate any image file. (I have given the permission to write in External Storage)

Bitmap bmp = Bitmap.createBitmap(640, 480, Bitmap.Config.ARGB_8888);//MUTABLE bitmap
            File file = new File(Environment.getExternalStorageDirectory(),"/image" + System.currentTimeMillis() + ".png");
            FileOutputStream str;
            try {

                str = new FileOutputStream(file);
                bmp.compress(Bitmap.CompressFormat.PNG,100,str);
            }
            catch (IOException e) {
                e.printStackTrace();
            }

This was just an attempt to check whether any image would be generated/stored or not.

1 Answers1

0

try closing the stream: str.close() after you write to it EDIT: since it worked, let me explain a bit: the stream keeps everything buffered in memory until some time (this isn't a totally deterministic thing), so if you close the stream it flushes everything it has buffered. So it's always a good practice to close streams.

fedeb
  • 122
  • 1
  • 4
  • Thanks a ton buddy. It is saving an image now but it is readable by ES File Explorer only and not by my phone gallery. And since not by my phone gallery, it is not able to be used in any other app. Could you suggest any alternative for it or a way to fix this? – kunal agarwal Jun 22 '17 at 13:29
  • maybe is simply a matter of path where you're saving the image? This https://stackoverflow.com/questions/15662258/how-to-save-a-bitmap-on-internal-storage uses another way for obtaining the path, hope it helps – fedeb Jun 23 '17 at 14:10
  • Thanks a lot, buddy. It sure helped. Corrected the path and it worked. – kunal agarwal Jun 24 '17 at 08:41