【问题标题】:Bitmap returns null pointer error Android位图返回空指针错误Android
【发布时间】:2014-01-30 17:23:34
【问题描述】:

我正在尝试从图库中选择图像并将其设置为 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();
                 }              
              }
            }
        }

生日计算.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!

【问题讨论】:

  • 请发布完整的logcat。
  • 您在哪一行收到此错误??
  • 贴了l​​ogcat代码,请看一下
  • @vipul mittal --- 位图 bitmap=BirthdayCalculation.getThumbnailBitmap(is, 200); byte bitObj[]=BirthdayCalculation.convertImageToByte(bitmap);
  • @karthik 检查我的回答。让我知道它是否适合您。

标签: android


【解决方案1】:

你确定你得到的是输入流吗,如果输入流为空,Bitmap.decodeStream 将返回空。

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

【讨论】:

  • 是的,我正在获取输入流。使用 Bitmap 时 bitmap=BitmapFactory.decodeStream(is, null, options)。它可以工作,但会引发内存不足错误
  • 如下commnetstackoverflow.com/a/21451157/1825844中提到的,同一个流不能被读取两次。您将必须制作副本,或者在您的情况下,从文件重新创建流。检查 ->stackoverflow.com/questions/18768422/…
  • 还可以考虑使用 AsyncTask 进行位图处理。位图处理不应该在 UI 线程上完成。
【解决方案2】:

您不能两次读取相同的流。尝试关注

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。

【讨论】:

  • 它在 Bitmap.createScaledBitmap(bitmap, inSampleSize , inSampleSize , false) 处抛出 Null 指针;
  • 你的 if 条件 if ((bounds.outWidth == -1) || (bounds.outHeight == -1)) 必须为真
【解决方案3】:

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

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

这意味着蒸汽将在一次读取后位于位图的末尾,这就是为什么下一次读取将失败并返回null,因此您最终会将null 传递给convertImageToByte(),这将失败尝试执行bitmap.compress(...) 时带有NullPointerException

在第二次调用 BitmapFactory#decodeStream() 之前,您必须 reset()(如果支持)或重新打开流。因为对于reset(),您需要缓冲整个位图,我建议您简单地关闭并重新打开流:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    相关资源
    最近更新 更多