I'm lookin开发者_JS百科g for the best way to download an array of Bitmaps, modify them a bit and then save to the SD card.
I've heard that ByteArrayOutputStream
is a bad idea to use because it loads the image into the RAM of the device. Instead I'm probably supposed to use something like a BufferedInputStream
and FileOutputStream
, however I don't know how I can alter the Bitmaps before saving them using this method.
Thanks
You will not like the answer: you cannot (at least, not with the Android framework).
The only option is to downscale the image so that it fits in memory; and regularly call recycle()
and System.gc()
to free memory as soon as possible.
You can create your bitmap from your InputStream
using:
Bitmap bm = BitmapFactory.decodeStream(inputStream);
and then after you have processed your Bitmap
and stored them you must use:
bm.recycle();
bm = null;
to avoid OutOfMemoryExceptions
.
EDIT:
For writing it to a file:
OutputStream fos=new BufferedOutputStream(new FileOutputStream(filePath));
byte buf[]=new byte[1024];
int len;
while((len=is.read(buf))>0)
fos.write(buf,0,len);
fos.close();
is.close();
精彩评论