从相机拍照时的Nullpointer异常

时间:2013-02-28 08:38:54

标签: android nullpointerexception android-camera

我试图从相机中捕捉到一张照片(三星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); 
                }

Log Cat:

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

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

我的代码适用于HTC Wildfire S,戴尔XCD35,三星Galaxy Grand和三星Galaxy Tab,对于S3中出现此错误的原因不太清楚。任何线索?

1 个答案:

答案 0 :(得分:2)

从你的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);
}