Search code examples
androidbitmapandroid-listviewimage-resizingandroid-image

Resize the bitmap


Im my project I am capturing the image from Camera app and displaying image in listview by using following code . MySimpleCursorAdapter:

public class MySimpleCursorAdapter extends SimpleCursorAdapter {

    public MySimpleCursorAdapter(Context context, int layout, Cursor c,
        String[] from, int[] to) {
        super(context, layout, c, from, to);
    }

    @Override
    public void setViewImage(ImageView image_v, String id) {

        String path = id;
        Bitmap b = BitmapFactory.decodeFile(path);
        image_v.setImageBitmap(resizedBitmap);

    }

}

List.java

String[] from = new String[]{DbManager.babyName, DbManager.baby_image}; 
        int[] to = new int[] {/*R.id.list_id,*/ R.id.drname12,R.id.imageView1};//,R.id.list_date};{                 
        MySimpleCursorAdapter   Adapter = new MySimpleCursorAdapter(this, R.layout.rowlayout, c1, from, to);       
        list.setAdapter(Adapter);

Every things is working fine till this point But when I try to resize the bitmap by using this piece of code

 Bitmap resizedBitmap = Bitmap.createScaledBitmap(b, 200, 200, true);

its giving following error :

05-22 07:13:07.562: E/AndroidRuntime(478): FATAL EXCEPTION: main
05-22 07:13:07.562: E/AndroidRuntime(478): java.lang.NullPointerException
05-22 07:13:07.562: E/AndroidRuntime(478):  at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:380)
05-22 07:13:07.562: E/AndroidRuntime(478):  at com.example.tottal.baby.care.MySimpleCursorAdapter.setViewImage(MySimpleCursorAdapter.java:22)
05-22 07:13:07.562: E/AndroidRuntime(478):  at android.support.v4.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:143)
05-22 07:13:07.562: E/AndroidRuntime(478):  at android.support.v4.widget.CursorAdapter.getView(CursorAdapter.java:256)

Solution

  • Error is here check this properly

     Bitmap b = BitmapFactory.decodeFile(path);
    

    your variable path or b may be null.

    check these variable not equal to null then use them.