Why does saving a bitmap take so long?
Asked Answered
M

1

9

So I have an app on Google Glass that takes a picture, then converts it to grayscale 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();
        }
    }

The grey bitmap is not fully visible in memory using Windows Photo Viewer until the Google Glass is unplugged from the debugging computer and then plugged back in. Sometimes half of the grayscale image can be viewed but that's it. I altered the code to show the image rather than save it into internal memory and this was quick and successful which makes me think that the problem is with reading the grayscale image into 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 explanations or suggestions welcome. Thanks.

Magnetograph answered 22/2, 2015 at 17:55 Comment(1)
"Why does saving a bitmap take so long?" -- use Traceview and find out. "The grey bitmap is not fully visible in memory using Windows Photo Viewer until the Google Glass is unplugged from the debugging computer and then plugged back in" -- use MediaScannerConnection and scanFile() to get the file indexed by the MediaStore. "Sometimes half of the grayscale image can be viewed but that's it" --call getFD().sync() on the FileOutputStream after you call flush() but before you call close().Itin
S
17

I am not using the Google Glass, but faced this same problem. So I am going to document my findings here.

My bitmap was taking a long time to save. It took me nearly 4 sec to save a bitmap of size 1200x1200 and the final saved file size was 2MB. But I didn't need that level of clarity and file size. When I mean "save", it means the actual saving to the disk alone and not the time taken by the Android OS to detect it as a media item.


ACTUAL SAVE: Try out the following if you find the saving of bitmap to be slow:

  1. Try to use JPEG format and use PNG only when it is absolutely necessary. Use bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
  2. If you are creating the bitmap and you don't care about the transparency, then use Bitmap.Config.RGB_565 instead of Bitmap.Config.ARGB_8888.

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


DETECTION IN FILE MANAGER:

Once you have saved the bitmap as a File in the Storage, the Android OS won't immediately show this file in the File Manager or in the explorer when you connect it to your PC. In order to do this detection fast, you need to use MediaScannerConnection.

MediaScannerConnection.scanFile(getActivity(), new String[]{file.toString()}, null, null);
Shipworm answered 23/10, 2015 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.