位图返回空指针错误Android

时间:2014-01-30 08:07:55

标签: android

我正在尝试从图库中选择图片并将其设置为 listview 中的 imageview ,因为我处理了一段代码。 此代码抛出NULL POINTER EXCEPTION ,我无法解决错误。请帮助我这个案例

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

错误

 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

3 个答案:

答案 0 :(得分:0)

你不能两次读同一个流。试试以下

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

读到InputStream后,它变为空,这就是为什么当您尝试再次读取它以进行解码时返回null。

答案 1 :(得分:0)

您确定要获取输入流吗?如果输入流为空,则Bitmap.decodeStream将返回null。

请参阅 - > `getContentResolver().openInputStream(uri)` throws FileNotFoundException

答案 2 :(得分:0)

getThumbnailBitmap()中,您使用相同的流拨打BitmapFactory#decodeStream()两次,就像@vipulmittal指出的那样。流不会变为“空”,但正如the documentation所解释的那样:

  

将输入流解码为位图。如果输入流为空,或者不能用于解码位图,则该函数返回null。 流的位置将是读取编码数据后的位置。

这意味着一次读取后蒸汽将位于位图的末尾,这就是下一次读取失败并返回null的原因,因此您最终会将null传递给{{ 1}}在尝试执行convertImageToByte()时,NullPointerException会失败。

您必须bitmap.compress(...)(如果支持)或重新打开流,然后再拨打reset()。因为BitmapFactory#decodeStream()你需要缓冲整个位图,我建议你简单地关闭并重新打开流:

reset()