Why does saving a bitmap take so long?

So, I have an application in Google Glass that takes a picture, then converts it to shades of gray and overwrites the original image in memory:

private void rGBProcessing (final String picturePath, Mat image) { //BitmapFactory Creates Bitmap objects from various sources, //including files, streams, and byte-arrays Bitmap myBitmapPic = BitmapFactory.decodeFile(picturePath); image = new Mat(myBitmapPic.getWidth(), myBitmapPic.getHeight(), CvType.CV_8UC4); Mat imageTwo = new Mat(myBitmapPic.getWidth(), myBitmapPic.getHeight(), CvType.CV_8UC1); Utils.bitmapToMat(myBitmapPic, image); Imgproc.cvtColor(image, imageTwo, Imgproc.COLOR_RGBA2GRAY); Utils.matToBitmap(imageTwo, myBitmapPic); FileOutputStream out = null; try { out = new FileOutputStream(picturePath); myBitmapPic.compress(Bitmap.CompressFormat.PNG, 100, out); // PNG is a lossless format, the compression factor (100) is ignored } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } 

A gray bitmap is not fully displayed in memory using the Windows Photo Viewer until Google Glass is disconnected from the debug computer and then reconnected. Sometimes you can view half the image in grayscale, but it is. I changed the code to show the image, not save it to the internal memory, and it was fast and successful, which makes me think that the problem is reading the grayscale image to the internal memory:

 FileOutputStream out = null; try { out = new FileOutputStream(picturePath); myBitmapPic.compress(Bitmap.CompressFormat.PNG, 100, out); // PNG is a lossless format, the compression factor (100) is ignored } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } 

Any explanation or suggestions are welcome. Thanks.

+3
source share
1 answer

I do not use Google Glass, but ran into the same problem. Therefore, I am going to document my results here.

My bitmap took a long time to save. It took me about 4 seconds to save a 1200x1200 bitmap and the final size of the saved file was 2 MB. But I do not need this level of clarity and file size. When I mean “save,” it means the actual save is only on disk, not the time it takes Android OS to identify it as a media item.


ACTUAL SAVING: Try the following if you find that saving a bitmap will be slow:

  • Try using the JPEG format and use PNG only when absolutely necessary. Use bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
  • If you are creating a bitmap and you do not need transparency, use Bitmap.Config.RGB_565 instead of Bitmap.Config.ARGB_8888 .

    Use Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);


FILE MANAGEMENT DETECTION:

Once you have saved the bitmap as File in the storage, Android OS will not immediately display this file in the File Manager or in Explorer when connected to a PC. To quickly complete this discovery, you need to use MediaScannerConnection .

 MediaScannerConnection.scanFile(getActivity(), new String[]{file.toString()}, null, null); 
+6
source

All Articles