I'm working on a media player app and wish to load album art images to display in a ListView. Right now it works fine with the images I'm auto-downloading from last.fm which are under 500x500 png's. However, I recently added another panel to my app that allows viewing full screen artwork so I've replaced some of my artworks with large (1024x1024) png's instead.
Now when I scroll over several albums with high res artwork, I get a java.lang.OutOfMemoryError on my BitmapFactory.
static public Bitmap getAlbumArtFromCache(String artist, String album, Context c)
{
Bitmap artwork = null;
File dirfile = new File(SourceListOperations.getAlbumArtPath(c));
dirfile.mkdirs();
String artfilepath = SourceListOperations.getAlbumArtPath(c) + File.separator + SourceListOperations.makeFilename(artist) + "_" + SourceListOperations.makeFilename(album) + ".png";
File infile = new File(artfilepath);
try
{
artwork = BitmapFactory.decodeFile(infile.getAbsolutePath());
}catch(Exception e){}
if(artwork == null)
{
try
{
artwork = BitmapFactory.decodeResource(c.getResources(), R.drawable.icon);
}catch(Exception ex){}
}
return artwork;
}
Is there anything I can add to limit the size of the resulting Bitmap object to say, 256x256? That's all the bigger the thumbnails need to be and I could make a duplicate function or an argument to fetch the full size artwork for displaying full screen.
Also, I'm displaying these Bitmaps on ImageViews that are small, around 150x150 to 200x200. The smaller images scale down nicer than the large ones do. Is there any way to apply a downscaling filter to smooth the image (anti-aliasing perhaps)? I don't want to cache a bunch of additional thumbnail files if I don't have to, because it would make managing the artwork images more difficult (currently you can just dump new ones in the directory and they will automatically be used next time they get loaded).
The full code is at http://github.org/CalcProgrammer1/CalcTunes, in src/com/calcprogrammer1/calctunes/AlbumArtManager.java, though there's not much different in the other function (which falls back to checking last.fm if the image is missing).
I use this private function to setup the size I want for my thumbnails:
//decodes image and scales it to reduce memory consumption
public static Bitmap getScaledBitmap(String path, int newSize) {
File image = new File(path);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inInputShareable = true;
options.inPurgeable = true;
BitmapFactory.decodeFile(image.getPath(), options);
if ((options.outWidth == -1) || (options.outHeight == -1))
return null;
int originalSize = (options.outHeight > options.outWidth) ? options.outHeight
: options.outWidth;
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = originalSize / newSize;
Bitmap scaledBitmap = BitmapFactory.decodeFile(image.getPath(), opts);
return scaledBitmap;
}
public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight)
{
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
{
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
Adapted from http://developer.android.com/training/displaying-bitmaps/load-bitmap.html but to use loads from files rather than resources. I've added a thumbnail option as such:
//Checks cache for album art, if it is not found return default icon
static public Bitmap getAlbumArtFromCache(String artist, String album, Context c, boolean thumb)
{
Bitmap artwork = null;
File dirfile = new File(SourceListOperations.getAlbumArtPath(c));
dirfile.mkdirs();
String artfilepath = SourceListOperations.getAlbumArtPath(c) + File.separator + SourceListOperations.makeFilename(artist) + "_" + SourceListOperations.makeFilename(album) + ".png";
File infile = new File(artfilepath);
try
{
if(thumb)
{
artwork = decodeSampledBitmapFromFile(infile.getAbsolutePath(), 256, 256);
}
else
{
artwork = BitmapFactory.decodeFile(infile.getAbsolutePath());
}
Yoann's answer looks pretty similar and is a bit more condensed, might use that solution instead, but that page had some good information on it about BitmapFactory.
One way to do this would be the AQuery library.
This is a library that allows you to lazy load images from either your local storage or an url. With support for things like caching and downscaling.
Example to lazy load a resource without downscaling:
AQuery aq = new AQuery(mContext);
aq.id(yourImageView).image(R.drawable.myimage);
Example to lazy load a image in a File object with downscaling:
InputStream ins = getResources().openRawResource(R.drawable.myImage);
BufferedReader br = new BufferedReader(new InputStreamReader(ins));
StringBuffer sb;
String line;
while((line = br.readLine()) != null){
sb.append(line);
}
File f = new File(sb.toString());
AQuery aq = new AQuery(mContext);
aq.id(yourImageView).image(f,350); //Where 350 is the width to downscale to
Example how to download from an url with local memory caching, local storage caching and resizing.
AQuery aq = new AQuery(mContext);
aq.id(yourImageView).image(myImageUrl, true, true, 250, 0, null);
This will start a async download of the image at myImageUrl,resize it to a 250 width and cache it in memory and storage.Then it will show the image in your yourImageView. Whenever the image of myImageUrl has been downloaded and cached before, this line of code will load the one cached in the memory or storage instead.
Usually these methods would be called in the getView method of a list adapter.
For full documentation on AQuery's image loading capabilities, you can check the documentation.
This is easily done with droidQuery:
final ImageView image = (ImageView) findViewById(R.id.myImage);
$.ajax(new AjaxOptions(url).type("GET")
.dataType("image")
.imageHeight(256)//set the output height
.imageWidth(256)//set the output width
.context(this)
.success(new Function() {
#Override
public void invoke($ droidQuery, Object... params) {
$.with(image).val((Bitmap) params[0]);
}
})
.error(new Function() {
#Override
public void invoke($ droidQuery, Object... params) {
droidQuery.toast("could not set image", Toast.LENGTH_SHORT);
}
}));
You can also cache the responses using the cache and cacheTimeout methods.
Related
I'm implementing ListView with Custom Adapter that extends BaseAdapter. In my app images are downloaded from URLs and then set as bitmap images in Listview.
The problem is that after complete downloading of 2 images java.Lang.OutOfMemoryError occurs and app crashes. I know this is memory issue but I don't know what steps should I take to avoid this... I worked with compressed images and they worked fine.
Below is my Code for getView() of CustomAdapter and doInBackground() of AsyncTask.
Thankx in advance...
Any suggestions will be appreciated..
public View getView(final int position, View convertView, final ViewGroup parent) {
if (convertView == null){
convertView = inflater.inflate(R.layout.row, parent, false);
}
img_name = "test" + position;
image = (ImageView) convertView.findViewById(R.id.imageView1);
Log.d("URL", ""+values[position]);
AsyncTaskRunner runner = new AsyncTaskRunner();
final int x = (int) getItemId(position);
runner.execute(values[x] , image , img_name);
return convertView;
}
doInBackground(Object... params)
protected ImageView doInBackground(Object... params) {
//publishProgress("Calculating..."); // Calls onProgressUpdate()
URL imageURL = null;
try {
url = (String) params[0];
img = (ImageView) params[1];
name = (String) params[2] + ".png";
imageURL = new URL(url);
Log.d("URL", ""+params[0]);
}
catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection connection= (HttpURLConnection)imageURL.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);
resized = Bitmap.createScaledBitmap(bitmap, 200, 200, true);
}
catch (IOException e) {
e.printStackTrace();
}
return img;
}
OnPostExecute(ImageView result)
protected void onPostExecute(ImageView result) {
result.setImageBitmap(resized);
}
java.Lang.OutOfMemoryError
Reason:
Each process in android is assigned a max heap size which varies from
device to device.(approx. 16 MB on avg.).And high resolution images when used in application occupies large space of this heap.So when a new instance of the bitmap is created and if total size exceeds the allocated heap size, this is error is thrown by the JVM.
Solution:
Android provide a way to handle this problem.Before decoding bitmap we just decode it with options.inJustDecodeBounds = true. where options is the instance of BitmapFactory. It does not load bitmap into memory but it help us to find the width and height of a bitmap so that we can reduce the height and width according to our device.
Then down-scale your bitmap to a create a smaller sized image which would in turn take up less space on the heap.
Here is the way to do it.
BitmapFactory.Options bmpBuffer = new BitmapFactory.Options();
bmpBuffer.inSampleSize = 3;
Bitmap bmp = BitmapFactory.decodeFile(path, bmpBuffer);
This would make your bitmaps 1/3 rd of the original size and hence
would take up 1/3rd space as well.
Eg.
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
Method to Calculate sample size:
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
Note:
One additional thing you can do is use following ,in case there is no other option available. However I want to caution you that it would affect other applications on your device. Hence I would not recommend you to use this.
<application
android:largeHeap="true">
</application>
largeHeap="true" will allow the application to use more heap if it is available.However your app will spend more time during garbage collection.Other apps on the device might get kicked out of memory.
CommonsWare has explained it here
You can use Picasso Library to download images from a url efficiently besides having more options to resize image size, cache, etc. ..
use this bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
I'm working on an app that uses large images (1390 × 870 : 150kb - 50kb). I'm adding images as I tap a trigger/ImageView.
At a certain point I'm getting an out of memory error:
java.lang.OutOfMemoryError
E/AndroidRuntime(23369): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
E/AndroidRuntime(23369): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:613)
E/AndroidRuntime(23369): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:378)
To resize the image I'm doing this:
Bitmap productIndex = null;
final String imageLoc = IMAGE_LOCATION;
InputStream imageStream;
try {
imageStream = new FileInputStream(imageLoc);
productIndex = decodeSampledBitmapFromResource(getResources(), imageLoc, 400, 400);
productIV.setImageBitmap(productIndex);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, String resId, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(resId, options);
}
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 3;
final int halfWidth = width / 3;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
I got this way of resizing to save space from the Android Docs:
Loading Large Bitmaps Efficiently
According to the log this like is the culprit in the decodeSampledBitmapFromResource method :
return BitmapFactory.decodeFile(resId, options);
----- edit -----
Here is how I'm adding each item to the FrameLayout.
for(int ps=0;ps<productSplit.size();ps++){
//split each product by the equals sign
List<String> productItem = Arrays.asList(productSplit.get(ps).split("="));
String tempCarID = productItem.get(0);
tempCarID = tempCarID.replace(" ", "");
if(String.valueOf(carID).equals(tempCarID)){
ImageView productIV = new ImageView(Configurator.this);
LayoutParams productParams = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
productIV.setId(Integer.parseInt(partIdsList.get(x)));
productIV.setLayoutParams(productParams);
final String imageLoc = productItem.get(2);
InputStream imageStream;
try {
imageStream = new FileInputStream(imageLoc);
productIndex = decodeSampledBitmapFromResource(getResources(), imageLoc, 400, 400);
productIV.setImageBitmap(productIndex);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
productLayers.addView(productIV);
}
}
You can use another bitmap-config to heavily decrease the size of the images. The default is RGB-config ARGB8888 which means four 8-bit channels are used (red, green, blue, alhpa). Alpha is transparency of the bitmap. This occupy a lot of memory - imagesize X 4. So if the imagesize is 4 megapixel 16 megabytes will immidiately be allocated on the heap - quickly exhausting the memory.
Instead - use RGB_565 which to some extent deteriorate the quality - but to compensate this you can dither the images.
So - to your method decodeSampledBitmapFromResource - add the following snippets:
options.inPreferredConfig = Config.RGB_565;
options.inDither = true;
In your code:
public static Bitmap decodeSampledBitmapFromResource(Resources res, String resId, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
options.inPreferredConfig = Config.RGB_565;
options.inDither = true;
return BitmapFactory.decodeFile(resId, options);
}
References:
http://developer.android.com/reference/android/graphics/Bitmap.Config.html#ARGB_8888
High resolution devices such as S4 usually run out of memory if you do not have your image in the proper folder which is drawable-xxhdpi. You can also put your image into drawable-nodpi. The reason it would run out of memorey if your image just in drawable that the android would scale the image thinking that the image was designed for low resolution.
You can use this beautiful library https://github.com/davemorrissey/subsampling-scale-image-view
Here is how I'm adding each item to the FrameLayout
that's the problem, the code keep adding and adding more images, and doesn't matter how well you resize or how much memory the device have, at certain point it WILL run out of memory. That's because every image you add it's keeping in memory.
For this type of situation what the apps do is to use a ViewGroup that can recycle views. I don't know your layout, but usually is a ListView, GridView or a ViewPager, by recycling views you re-use the layout and can dispose re-load images as necessary.
For the specific purpose of loading and resizing images I strongly advise use Picasso library as it is VERY well written, simple to use and stable.
You are still going to need to manage the bitmap memory as I wouldn't try to allocate a total space more than 3x the size of the screen (if you think about it makes sense for scrolling behavior). If you are overlaying one image on top of another, at some point, you're hitting an Out Of Memory error. You may need to look at capturing the prior screen image as a single background image to make sure you still fit within the available memory. Or when a new image overlaps an existing image only load and render the visible portion. If performance becomes an issue, then you may need to consider OpenGL Textures but the memory allocation problem is still the same.
Do go through all of the Displaying Bitmaps Training as it should give you some additional ideas of how to handle display.
Use Fresco library to load large images will avoid this error.
in xml layout
<com.facebook.drawee.view.SimpleDraweeView
android:id="#+id/my_image_view"
android:layout_width="1300dp"
android:layout_height="1300dp"
fresco:placeholderImage="#drawable/my_drawable"
/>
and in javacode
Uri uri = Uri.parse("https://image.png");
SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);
draweeView.setImageURI(uri);
My app is an OCR app base on Tesseract. It will do OCR task from camera picture. Users can take many pictures and put them into an OCR queue. To get more accuracy, I want to keep high quality image (I choose min size is 1024 x 768 (maybe larger in future), JPEG, 100% quality). When users take many pictures, there are three things to do:
Save the image data byte[] to file and correct EXIF.
Correct the image orientation base on device's orientation. I know there are some answers that said the image which comes out of the camera is not oriented automatically, have to correct it from file, like here and here. I'm not sure about it, I can setup the camera preview orientation correctly, but the image results aren't correct.
Load bitmap from taken picture, convert it to grayscale and save to another file for OCR task.
And here is my try:
public static boolean saveBitmap(byte[] bitmapData, int orientation, String imagePath, String grayScalePath) throws Exception {
Boolean rotationSuccess = false;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap originalBm = null;
Bitmap bitmapRotate = null;
Bitmap grayScale = null;
FileOutputStream outStream = null;
try {
// save directly from byte[] to file
saveBitmap(bitmapData, imagePath);
// down sample
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imagePath, options);
int sampleSize = calculateInSampleSize(options, Config.CONFIG_IMAGE_WIDTH, Config.CONFIG_IMAGE_HEIGHT);
options.inJustDecodeBounds = false;
options.inSampleSize = sampleSize;
originalBm = BitmapFactory.decodeFile(imagePath, options);
Matrix mat = new Matrix();
mat.postRotate(orientation);
bitmapRotate = Bitmap.createBitmap(originalBm, 0, 0, originalBm.getWidth(), originalBm.getHeight(), mat, true);
originalBm.recycle();
originalBm = null;
outStream = new FileOutputStream(new File(imagePath));
bitmapRotate.compress(CompressFormat.JPEG, 100, outStream);
// convert to gray scale
grayScale = UIUtil.convertToGrayscale(bitmapRotate);
saveBitmap(grayScale, grayScalePath);
grayScale.recycle();
grayScale = null;
bitmapRotate.recycle();
bitmapRotate = null;
rotationSuccess = true;
} catch (OutOfMemoryError e) {
e.printStackTrace();
System.gc();
} finally {
if (originalBm != null) {
originalBm.recycle();
originalBm = null;
}
if (bitmapRotate != null) {
bitmapRotate.recycle();
bitmapRotate = null;
}
if (grayScale != null) {
grayScale.recycle();
grayScale = null;
}
if (outStream != null) {
try {
outStream.close();
} catch (IOException e) {
}
outStream = null;
}
}
Log.d(TAG,"save completed");
return rotationSuccess;
}
Save to file directly from byte[]
public static void saveBitmap(byte[] bitmapData, String fileName) throws Exception {
File file = new File(fileName);
FileOutputStream fos;
BufferedOutputStream bos = null;
try {
final int bufferSize = 1024 * 4;
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos, bufferSize);
bos.write(bitmapData);
bos.flush();
} catch (Exception ex) {
throw ex;
} finally {
if (bos != null) {
bos.close();
}
}
}
Calculate scale size
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and
// keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
When save complete, this image is loaded into thumbnail image view by UIL. The problem is the save task is very slow (wait some second before save complete and load into view), and sometime I got OutOfMemory exception. Is there any ideas to reduce the save task and avoid OutOfMemory exception?
Any help would be appreciated!
P/S: the first time I try to convert byte[] to bitmap instead of save to file, and then rotate and convert to grayscale, but I still got above issues.
Update: here is the grayscale bitmap process:
public static Bitmap convertToGrayscale(Bitmap bmpOriginal) {
int width, height;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bmpOriginal, 0, 0, paint);
return bmpGrayscale;
}
The OutOfMemory exception seldom occurred (just a few times) and I can't reproduce it now.
Update:
Since you're still saying that the method takes too long time I would define a callback interface
interface BitmapCallback {
onBitmapSaveComplete(Bitmap bitmap, int orientation);
onBitmapRotateAndBWComlete(Bitmap bitmap);
}
Let your activity implement the above interface and convert the byte[] to bitmap in top of your saveBitmap method and fire the callback, before the first call to save. Rotate the imageView based on the orientation parameter and set a black/white filter on the imageView to fool the user into thinking that the bitmap is black and white (do this in your activity). See to that the calls are done on main thread (the calls to imageView). Keep your old method as you have it. (all steps need to be done anyway) Something like:
public static boolean saveBitmap(byte[] bitmapData, int orientation, String imagePath, String grayScalePath, BitmapCallback callback) throws Exception {
Boolean rotationSuccess = false;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap originalBm = null;
Bitmap bitmapRotate = null;
Bitmap grayScale = null;
FileOutputStream outStream = null;
try {
// TODO: convert byte to Bitmap, see to that the image is not larger than your wanted size (1024z768)
callback.onBitmapSaveComplete(bitmap, orientation);
// save directly from byte[] to file
saveBitmap(bitmapData, imagePath);
.
.
// same as old
.
.
saveBitmap(grayScale, grayScalePath);
// conversion done callback with the real fixed bitmap
callback.onBitmapRotateAndBWComlete(grayScale);
grayScale.recycle();
grayScale = null;
bitmapRotate.recycle();
bitmapRotate = null;
rotationSuccess = true;
How do you setup your camera? What might be causing the long execution time in the first saveBitmap call, could be that you are using the default camera picture size settings and not reading the supported camera picture size and choosing best fit for your 1024x768 image needs. You might be taking big mpixel images and saving such, but in the end need you need < 1 mpixles (1024x768). Something like this in code:
Camera camera = Camera.open();
Parameters params = camera.getParameters();
List sizes = params.getSupportedPictureSizes();
// Loop camera sizes and find best match, larger than 1024x768
This is probably where you will save most of the time if you are not doing this already. And do it only once, during some initialization phase.
Increase the buffer to 8k in saveBitmap, change the 1024*4 to 1024*8, this would increase the performance at least, not save any significant time perhaps.
To save/reuse bitmap memory consider using inBitmap field, if you have a post honeycomb version, of BitmapFactory.Options and set that field to point to bitmapRotate bitmap and send options down to your convertToGrayscale method to not need allocating yet another bitmap down in that method. Read about inBitmap here: inBitmap
I am using bitmaps. When the code runs it shows an out of memory error. How can the error be avoided. My code follows. Thanks in advance.
Bitmap myBitmap = Image.decodeSampledBitmapFromUri(path, 250, 500);
img_cook[index].setImageBitmap(myBitmap);
public static Bitmap decodeSampledBitmapFromUr(String path, int reqWidth,
int reqHeight) {
Bitmap bm = null;
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(path, options);
return bm;
}
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
When you have done with your Bitmap, means when your Bitmap done its work then make it recyle and null like below:
bitmap.recycle();
bitmap=null;
OR
I think you are downloading Image from url, so I am suggesting you to use Android Query for this, you will never get this error if you used it.
You can download the jar file from here :
http://code.google.com/p/android-query/downloads/list
Download the jar file and set jar to your Build Path.
AQuery androidAQuery=new AQuery(this);
As an example to load image directly from url:
androidAQuery.id(YOUR IMAGEVIEW).image(YOUR IMAGE TO LOAD, true, true, getDeviceWidth(), ANY DEFAULT IMAGE YOU WANT TO SHOW);
As an example to get Bitmap from url:
androidAQuery.ajax(YOUR IMAGE URL,Bitmap.class,0,new AjaxCallback<Bitmap>(){
#Override
public void callback(String url, Bitmap object, AjaxStatus status) {
super.callback(url, object, status);
//You will get Bitmap from object.
}
});
It's very fast and accurate, and using this you can find many more features like Animation when loading; getting a bitmap, if needed; etc.
Still now your image size are big that why use width and height like that and after set the image the clear the chache
Bitmap myBitmap = Image.decodeSampledBitmapFromUri(path, 60, 60);
img_cook[index].setImageBitmap(myBitmap);
if (myBitmap != null)
{
bitmap.recycle();
bitmap = null;
System.gc();
}
Im guessing you're not getting the OOM exception after you create your first bitmap, but rather, this happens after you load several bitmaps into memory?
Try to improve your efficiency by manually calling recycle() on bitmaps you no longer need. While the GC collects some data of Bitmaps which have all their references freed, the actual memory of the image is stored in native memory, and so a call to bitmap.recycle() is required to release this memory when you need it to be released.
Hope this helps.
Android applications have very low amount of memory. You should manage it carefully to avoid out of memory exception. You can see the
Google's Solution
Using resources from StackOverflow and other helpful websites, I was successful in creating an application that can upload an image taken by the camera application on an Android phone. The only trouble is, the phone I have right now takes very high-quality pictures, resulting in a long wait-time for uploads.
I read about converting images from jpeg to a lower rate (smaller size or just web-friendly sizes), but the code I am using right now saves the captured image as a byte (see code below). Is there any way to reduce the quality of the image in the form that it is in, or do I need to find a way to convert it back to jpeg, reduce the image quality, and then place it back in byte form?
Here is the code snippet I'm working with:
if (Intent.ACTION_SEND.equals(action)) {
if (extras.containsKey(Intent.EXTRA_STREAM)) {
try {
// Get resource path from intent callee
Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
// Query gallery for camera picture via
// Android ContentResolver interface
ContentResolver cr = getContentResolver();
InputStream is = cr.openInputStream(uri);
// Get binary bytes for encode
byte[] data = getBytesFromFile(is);
// base 64 encode for text transmission (HTTP)
int flags = 1;
byte[] encoded_data = Base64.encode(data, flags);
// byte[] encoded_data = Base64.encodeBase64(data);
String image_str = new String(encoded_data); // convert to
// string
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",
image_str));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://xxxxx.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String the_string_response = convertResponseToString(response);
Toast.makeText(UploadImage.this,
"Response " + the_string_response,
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(UploadImage.this, "ERROR " + e.getMessage(),
Toast.LENGTH_LONG).show();
System.out.println("Error in http connection "
+ e.toString());
}
}
}
}
For web apps, you definitely don't need the 5+ MP images that cameras produce; image resolution is the primary factor in image size, so I'd suggest you use the BitmapFactory class to produce a downsampled bitmap.
Particularly, look at BitmapFactory.decodeByteArray(), and pass it a BitmapFactory.Options parameter indicating you want a downsampled bitmap.
// your bitmap data
byte[] rawBytes = .......... ;
// downsample factor
options.inSampleSize = 4; // downsample factor (16 pixels -> 1 pixel)
// Decode bitmap with inSampleSize set
return BitmapFactory.decodeByteArray(rawBytes, 0, rawBytes.length, options);
For more info, take a look at the Android Training lesson on displaying bitmaps efficiently and the reference for BitmapFactory:
http://developer.android.com/training/displaying-bitmaps/index.html
http://developer.android.com/reference/android/graphics/BitmapFactory.html
To tell the decoder to subsample the image, loading a smaller version into memory, set inSampleSize to true in your BitmapFactory.Options object. For example, an image with resolution 2048x1536 that is decoded with an inSampleSize of 4 produces a bitmap of approximately 512x384. Loading this into memory uses 0.75MB rather than 12MB for the full image (assuming a bitmap configuration of ARGB_8888). see this
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
public Bitmap decodeSampledBitmapFromResource(
String pathName) {
int reqWidth,reqHeight;
reqWidth =Utils.getScreenWidth();
reqWidth = (reqWidth/5)*2;
reqHeight = reqWidth;
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// BitmapFactory.decodeStream(is, null, options);
BitmapFactory.decodeFile(pathName, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(pathName, options);
}
public int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float) height / (float) reqHeight);
} else {
inSampleSize = Math.round((float) width / (float) reqWidth);
}
}
return inSampleSize;
}