【问题标题】:Nullpointer exception while taking picture from camera从相机拍照时出现空指针异常
【发布时间】:2013-02-28 08:38:53
【问题描述】:

我正在尝试从相机拍摄照片(三星 S3 特定问题)下面是我的代码:

      protected void onActivityResult(int requestCode, int resultCode, Intent data) {  



    if( requestCode == 1337 && resultCode== RESULT_OK){
        Bundle extras = data.getExtras();
            if (extras != null){    
                   BitmapFactory.Options options = new BitmapFactory.Options();
            // options.inSampleSize = 1;
            // options.inPurgeable = true;
            // options.inInputShareable = true;
             thumbnail = (Bitmap) extras.get("data");
             image(thumbnail);
    }else{
                Toast.makeText(CreateProfile.this, "Picture NOt taken", Toast.LENGTH_LONG).show();
         } 

图像功能:

     public void image(Bitmap thumbnail){
       Bitmap photo = thumbnail; 
       ByteArrayOutputStream bos = new ByteArrayOutputStream();
       photo.compress(Bitmap.CompressFormat.PNG, 100, bos);
       b = bos.toByteArray();
       ImageView imageview = (ImageView)findViewById(R.id.imageView1);
      }

启动相机意图的代码:

       if(i==0){
                       Intent cameraIntent = new  Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                       startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); 
                }

原木猫:

http://img534.imageshack.us/img534/8388/logcamera.png

http://img585.imageshack.us/img585/7559/camera1u.png

我的代码在 HTC Wildfire S、Dell XCD35、Samsung Galaxy Grand 和 Samsung Galaxy Tab 上运行良好,但不知道为什么在 S3 中显示此错误。有什么线索吗?

【问题讨论】:

  • 相机启动意图的邮政编码..
  • 我认为thumbnail = (Bitmap) extras.get("data"); 会返回NULL 位图。
  • Yes @ user370305 当我注释掉该 image() 语句时,它可以正常工作。第 1352 行是对 image() 的调用;
  • 将 logcat 发布为文本而不是图像!

标签: android nullpointerexception android-camera


【解决方案1】:

从您的LOGCAT 输出看来,相机活动似乎正在返回所拍摄照片的 URI。并且没有缩略图,只是为了确保使用:

if (extras.keySet().contains("data") ){
       thumbnail = (Bitmap) extras.get("data");
       image(thumbnail);
}

关于解析 Uri 在意图中您可以使用以下内容:

Uri imageURI = getIntent().getData();
ImageView imageview = (ImageView)findViewById(R.id.imageView1);
imageview.setImageURI(imageURI);

所以完整的代码看起来像这样:

    if (extras.keySet().contains("data") ){
       thumbnail = (Bitmap) extras.get("data");
       image(thumbnail);
} else {
    Uri imageURI = getIntent().getData();
    ImageView imageview = (ImageView)findViewById(R.id.imageView1);
    imageview.setImageURI(imageURI);
}

【讨论】:

  • Bundle 类型的 getKeys() 方法未定义
  • 它不能解决问题,因为它不能满足if条件为真。
  • 它解决了这个问题,因为 Galaxy S3 不返回额外的“数据”。所以不再崩溃,而是您应该使用传递给您的 uri 来手动获取预览
  • 如何获取 URI?有什么见解吗?
  • 男人爱菲拉斯汀,也爱你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-09
相关资源
最近更新 更多