1

I am trying to pick image from gallery and setting it to the imageview in the listview, for that I working on a piece of code. This code throws NULL POINTER EXCEPTION , I am not able to solve the error. pls help me in this case

ContactInfoMoreOption.java

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==GALLERY_REQUEST) {
            if (resultCode==RESULT_OK) {                
                View view=getLayoutInflater().inflate(R.layout.list_row,null);
                ImageView imgView=(ImageView)view.findViewById(R.id.list_image);
                InputStream is = null;
                try {
                    is = getContentResolver().openInputStream(data.getData());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Bitmap bitmap=BirthdayCalculation.getThumbnailBitmap(is, 200);
                byte bitObj[]=BirthdayCalculation.convertImageToByte(bitmap);   
                ContentValues values=new ContentValues();
                values.put(BirthdayProvider.PHOTO, bitObj);
                int count=getContentResolver().update(BirthdayProvider.CONTENT_URI, values, BirthdayProvider.NUMBER+"='"+SearchListActivity.longClickValue+"'", null);
                if (count==1) {
                    finish();
                    imgView.setImageBitmap(bitmap);
                    imgView.setScaleType(ScaleType.FIT_XY);
                    Log.v("Photo Updated Successfully", "Photo Updated Successfully");
                    Toast.makeText(getBaseContext(),"Updated Successfully",Toast.LENGTH_SHORT).show();
                }
                else{
                     Toast.makeText(getBaseContext(),"Updation Failed",Toast.LENGTH_SHORT).show();
                 }              
              }
            }
        }

BirthdayCalculation.java

public static byte[] convertImageToByte(Bitmap bitmap){
      ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
      bitmap.compress(CompressFormat.PNG,0,outputStream);
      return outputStream.toByteArray();
  }


public static Bitmap getThumbnailBitmap(InputStream is, final int thumbnailSize) {
        Bitmap bitmap;
        BitmapFactory.Options bounds = new BitmapFactory.Options();
        bounds.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is,null, bounds);
        if ((bounds.outWidth == -1) || (bounds.outHeight == -1)) {
            bitmap = null;
        }
        int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight
                : bounds.outWidth;
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inSampleSize = originalSize / thumbnailSize;
        bitmap = BitmapFactory.decodeStream(is, null, opts);
        return bitmap;
    }

Error

 java.lang.RuntimeException: Failure delivering result
 ResultInfo{who=null, request=1, result=-1, data=Intent {
 dat=content://media/external/images/media/12532 }} to activity
 {com.android.project.birthdayreminder/com.android.project.birthdayreminder.ContactInfoMoreOption}:
 java.lang.NullPointerException

Caused by: java.lang.NullPointerException
    at com.android.project.birthdayreminder.BirthdayCalculation.convertImageToByte(BirthdayCalculation.java:565)
    at com.android.project.birthdayreminder.ContactInfoMoreOption.onActivityResult(ContactInfoMoreOption.java:720)
    at android.app.Activity.dispatchActivityResult(Activity.java:5192)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3145)

Logcat Error Link!

Melquiades
  • 8,496
  • 1
  • 31
  • 46
karthik
  • 165
  • 14

3 Answers3

0

you cannot read the same stream twice. Try following

public static Bitmap getThumbnailBitmap(InputStream is, final int thumbnailSize) {
    Bitmap bitmap;
    BitmapFactory.Options bounds = new BitmapFactory.Options();
    bounds.inJustDecodeBounds = true;
    bitmap= BitmapFactory.decodeStream(is,null, bounds);
    if ((bounds.outWidth == -1) || (bounds.outHeight == -1)) {
        bitmap = null;
    }
    int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight
            : bounds.outWidth;
    int inSampleSize = originalSize / thumbnailSize;
    Bitmap.createScaledBitmap(bitmap, inSampleSize , inSampleSize , false);   
    return bitmap;
}

Once you read an InputStream it becomes empty that is why when you are trying to read it again to decode it returns null.

vipul mittal
  • 17,343
  • 3
  • 41
  • 44
0

Are you sure you are getting the inputstream, The Bitmap.decodeStream will return null if the inputstream is null.

See ->`getContentResolver().openInputStream(uri)` throws FileNotFoundException

Community
  • 1
  • 1
gvmani
  • 1,580
  • 1
  • 12
  • 20
  • yes I am getting inputstream. When used Bitmap bitmap=BitmapFactory.decodeStream(is, null, options). It works ,but throws outofmemory error – karthik Jan 30 '14 at 08:40
  • As mentioned in below commnet http://stackoverflow.com/a/21451157/1825844, the same stream cannot be read twice. You will have to make a copy or in your case, recreate the stream from the file. Check ->http://stackoverflow.com/questions/18768422/reuse-inputstream-when-bitmapfactory-decodestreamstream-null-options – gvmani Jan 30 '14 at 08:48
  • Also consider using AsyncTask for bitmap processing. Bitmap processing is not supposed to be done on a UI thread. – gvmani Jan 30 '14 at 08:51
0

In getThumbnailBitmap() you call BitmapFactory#decodeStream() twice with the same stream, as @vipulmittal pointed out. The stream doesn't get "empty" but as the documentation explains:

Decode an input stream into a bitmap. If the input stream is null, or cannot be used to decode a bitmap, the function returns null. The stream's position will be where ever it was after the encoded data was read.

Which means that the steam will be at the end of the bitmap after one read, that's why the next read will fail and return null, so you'll end up passing null to convertImageToByte() which will fail with a NullPointerException when trying to execute bitmap.compress(...).

You must reset() (if supported) or re-open the stream before calling BitmapFactory#decodeStream() a 2nd time. Since for reset() you'd need to buffer the whole bitmap, I would recommend you to simply close and re-open the stream:

public Bitmap getThumbnailBitmap(final Uri uri, final int thumbnailSize) throws FileNotFoundException, IOException {
    InputStream is = null;
    final int originalSize;
    try {
        is = getContentResolver().openInputStream(uri);

        final BitmapFactory.Options bounds = new BitmapFactory.Options();
        bounds.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is, null, bounds);
        originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight : bounds.outWidth;
    } finally {
        if (is != null) {
            is.close();
        }
    }

    is = null;
    try {
        is = getContentResolver().openInputStream(uri);

        final BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inSampleSize = originalSize / thumbnailSize;
        return BitmapFactory.decodeStream(is, null, opts);
    } finally {
        if (is != null) {
            is.close();
        }
    }
}
desseim
  • 7,968
  • 2
  • 24
  • 26