1

I have 100 images on external memory which i do these two following tasks in a for loop .

  1. Loading every item as a bitmap and merging it with another bitmap
  2. Saving result in as a new file in memory

And for 100 images it takes too much time . Merging bitmaps is quit fast and OK but saving the result in file takes too much time . Is there anyway to boost this issue ? Keeping the bitmaps in memory and batch save files can cause OutOfMemoryException .

This is how i merge bitmaps : how to merge to two bitmap one over another

This is how i save the bitmap to file :

       File imageFileFolder = new File(Statics.TEMP_PATH);
    imageFileFolder.mkdirs();
    FileOutputStream out = null;
    File imageFileName = new File(imageFileFolder, imageName);
    try {
        out = new FileOutputStream(imageFileName);
        imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

Note that all of these are in a AsyncTask block .

Community
  • 1
  • 1
Aerox
  • 353
  • 3
  • 13
  • you could try whether adding a buffer, i.e. `out = new BufferedOutputStream(new FileOutputStream(imageFileName), 8 * 1024);` (try larger size as well) has any measurable effect. – zapl Apr 03 '17 at 09:32
  • What is too much time? Please tell how many seconds. Also tell file sizes. Use the cores of the cpu and use threads. One thread for every core. Or two. – greenapps Apr 03 '17 at 09:49
  • @greenapps saving file is an expensive task . if you have any solution please tell me and i will tell you the difference . Because the data set is not very important . – Aerox Apr 03 '17 at 09:56
  • @zapl ok i will check – Aerox Apr 03 '17 at 09:57
  • You did not give the info i asked for. And i told you already a solution. – greenapps Apr 03 '17 at 10:07
  • according to this : stackoverflow.com/questions/8712957/… Using BufferedOutputStream makes it worst . Because file sizes are about 150kb each @zapl – Aerox Apr 03 '17 at 10:22

1 Answers1

0

Finally i came with the solution to save Bitmap as jpeg. Saving a 250k png bitmap took almost 500 milliseconds . The same bitmap in jpeg format took about 50 milliseconds. Although the qualities wont be the same but it looks like it is a tradeoff between quality and performance.

Aerox
  • 353
  • 3
  • 13