I want to save 2 bitmap into a valid image file side by side.
I can save all of bytes from booth bitmaps and the image file is a valid image byte and i can open and see that , but just first bitmap showed in the Image!
I tried this :
void save_bitmaps()
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/req_images");
myDir.mkdirs();
String fname = "Map"+ String.valueOf(10) + ".png";
File file = new File(myDir, fname);
for(int i=0;i<2;i++)
{
int count=0;
//------------------------------------------
Bitmap bitmap = Bitmap.createBitmap(500,500,Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
if(i==0)
c.drawColor(Color.BLUE);
if(i==1)
c.drawColor(Color.RED);
bitmap.compress(Bitmap.CompressFormat.PNG,100,stream);
byte[] bytarray = stream.toByteArray();
//----------------------------------------------------------------
if (file.exists())
file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
out.write(bytarray,count,bytarray.length);
out.flush();
out.close();
count+=bytarray.length;
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getContext() , "ERROR" , Toast.LENGTH_SHORT).show();
}
}
Toast.makeText(getContext() ,"Saved" , Toast.LENGTH_SHORT).show();
}
this code worked but not show both of them!
this is my aim :
how i can do this . please help if you know how i can do this!
NOTE : 1. I never want to combine 2 bitmaps into another bitmap and save the target bitmap.
Thanks all!
If you wish, you can use this library I've made that can handle large image files via JNI :
https://github.com/AndroidDeveloperLB/AndroidJniBitmapOperations
But, it doesn't have this functionality that you wish. You will have to add it on your own:
You could even have just a single Java Bitmap object at the same time, instead of having 3 in the Java solution above (2 of input + 1 of output).
Some notes:
Using JNI can be slower, as it needs to convert from Java Bitmap object to JNI and back. This is especially true for large bitmaps.
In the end, you will still have to hold the output Java Bitmap object. If it's 10000x10000 pixels, it will take 10000*10000*4 bytes, which is 400,000,000 bytes, which is ~400MB . If the input images do not have transparency, you can use a different bitmap config (RGB_565) , which will take ~200MB of RAM.
In both cases, this is a lot. You could add a flag in the manifest to tell the OS that you need a lot of heap memory (largeHeap) , but it isn't a safe way to get it, as you still might not get enough heap memory. You should check the amount of available heap memory before trying to create the output bitmap.
Another way to solve this, is to avoid showing the entire bitmap, because there is no screen on Android device that has this resolution. You could instead just show a part of the bitmap to be shown on screen, and the calculation stuff you could leave in JNI.
If my library isn't enough, there are other libraries that use JNI. I'm sure one of them can help.