Stacy Blue Stacy Blue - 19 days ago 5
Android Question

How to save a run-time-created bitmap in Android?

In my application a user can upload some pictures and for database size limits I would like to merge these images to create only one image, I do that with:

private Bitmap combineImageIntoOne(ArrayList<Bitmap> bitmap) {
int w = 0, h = 0;
for (int i = 0; i < bitmap.size(); i++) {
if (i < bitmap.size() - 1) {
w = bitmap.get(i).getWidth() > bitmap.get(i + 1).getWidth() ? bitmap.get(i).getWidth() : bitmap.get(i + 1).getWidth();
}
h += bitmap.get(i).getHeight();
}

Bitmap temp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(temp);
int top = 0;
for (int i = 0; i < bitmap.size(); i++) {
Log.d("HTML", "Combine: "+i+"/"+bitmap.size()+1);

top = (i == 0 ? 0 : top+bitmap.get(i).getHeight());
canvas.drawBitmap(bitmap.get(i), 0f, top, null);
}
return temp;
}


But how do I save the image after that to look like this one?

Answer

Bitmap.compress is the method you are looking for - it allows you to save Bitmap to any OutputStream you want.

bitmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(someFile));