This is featured post 1 title

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation test link ullamco laboris nisi ut aliquip ex ea commodo consequat.

This is featured post 2 title

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation test link ullamco laboris nisi ut aliquip ex ea commodo consequat.

This is featured post 3 title

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation test link ullamco laboris nisi ut aliquip ex ea commodo consequat.

This is featured post 4 title

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation test link ullamco laboris nisi ut aliquip ex ea commodo consequat.

This is featured post 5 title

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation test link ullamco laboris nisi ut aliquip ex ea commodo consequat.


Custom Search
Showing posts with label Advance Android. Show all posts
Showing posts with label Advance Android. Show all posts

Displaying Bitmaps Efficiently and Avoiding java.lang.OutofMemoryError

0 comments

java.lang.OutofMemoryError: bitmap size exceeds VM budget. This is most common error for us when we are decoding Bitmap more than 4 MB. In generally before decoding bitmap we do not know how much bigger the size of a bitmap will be. So its very difficult problem for us to handle this error in proactive approach

But good thing is that android provide a way to handle this problem.Before decoding bitmap we just decode it with options.inJustDecodeBounds = true. 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

As Bitmaps take up a lot of memory, especially for rich images like photographs. For example, the camera on the Galaxy Nexus takes photos up to 2592x1936 pixels (5 megapixels). If the bitmap configuration used is ARGB_8888 (the default from the Android 2.3 onward) then loading this image into memory takes about 19MB of memory (2592*1936*4 bytes), immediately exhausting the per-app limit on some devices.

So we will find actual height and width of a bitmap as follows...


BitmapFactory.Options options = new BitmapFactory.Options();
options
.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;

Now scale down the bitmap and load into memory.I use decodeResource() method here but you can use any method(decodeFile etc).So now using following function scale down bitmap


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;
}

Here inSampleSize will reduce the size and memory size of an Bitmap.To use this method, first decode with inJustDecodeBounds set to true, pass the options through and then decode again using the new inSampleSize value and inJustDecodeBounds set to false.



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);
}


That is enough to avoid memory over flow error

Home screen widget , Weather widget in android

5 comments

Home-Screen widget are best when your application is running in background and you want to show progress update and related information on home screen.
For example-You are playing song in MediaPlayer and want to show progress on home screen about currently playing song and other information


So for this we need three classes and in the end of this article i have posted full source code link


  • WeatherDataProvider — Our ContentProvider which stores the weather data for this sample. Note that for simplicity, it currently stores the data in memory as opposed to an external and persistent storage such as a file, network location, database, or shared preference.
  • WeatherWidgetProvider — Our AppWidgetProvider which handles specific intent actions (such as when an item is clicked, or the refresh button is pressed). It also sets up the RemoteViews for the widget and binds the service to the collection view in the widget.
  • WeatherWidgetService — Our RemoteViewsService which manages the creation of new factories, which return the RemoteViews for each item in the ListView.   
Screen shot of this application will be like that



Note- This project is for android API version 3.0 and above so make sure your project have this level of API else it will show error


When you will run this.It has no launcher activity so it will give any out put.It will just show in console following message

[2012-04-17 17:43:41 - WeatherWighet] Android Launch!
[2012-04-17 17:43:41 - WeatherWighet] adb is running normally.
[2012-04-17 17:43:41 - WeatherWighet] No Launcher activity found!
[2012-04-17 17:43:41 - WeatherWighet] The launch will only sync the application package on the device!
[2012-04-17 17:43:41 - WeatherWighet] Performing sync
[2012-04-17 17:43:41 - WeatherWighet] Automatic Target Mode: using existing emulator 'emulator-5556' running compatible AVD 'android_14.0'
[2012-04-17 17:43:41 - WeatherWighet] Uploading WeatherWighet.apk onto device 'emulator-5556'
[2012-04-17 17:43:42 - WeatherWighet] Installing WeatherWighet.apk...
[2012-04-17 17:43:46 - WeatherWighet] Success!
[2012-04-17 17:43:46 - WeatherWighet] /WeatherWighet/bin/WeatherWighet.apk installed on device
[2012-04-17 17:43:46 - WeatherWighet] Done!

That is means that your HomeScreen widegt is install.you can show it on your main screen

                                          Download complete Source code