2012-05-23 52 views
0

什么在我的代码中的错误,使NullPointerException在这一行 data.getData();建在android摄像头

Handler handler = new Handler() { 

    @Override 
    public void handleMessage(Message msg) { 
     super.handleMessage(msg); 
     pd.dismiss(); 
     // startActivity(new Intent(LoginActivity.this, 
     // CameraActivity.class)); 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); 

     intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 
     startActivityForResult(intent,CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 
    } 
}; 

我在线程

,当捕捉图像使用此处理程序,并使用onActivityForResult异常发生。

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) { 
    if (resultCode == RESULT_OK) { 
     // Image captured and saved to fileUri specified in the Intent 
     Toast.makeText(this, "Image saved to:\n" + 
       data.getData(), Toast.LENGTH_LONG).show(); 
    } else if (resultCode == RESULT_CANCELED) { 
     // User cancelled the image capture 
    } else { 
     // Image capture failed, advise user 
    } 
} 

}

+0

以及它似乎你的数据对象是NULL ..但请记住,使用未记录的方法可能会导致你崩溃。 (指使用相机应用拍摄你的照片) – Raykud

回答

0

检查以下

public class Camera extends Activity 
    { 
private static final int CAMERA_REQUEST = 1888; 
private String selectedImagePath; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    Intent cameraIntent = new Intent(ACTION_IMAGE_CAPTURE); 
    startActivityForResult(cameraIntent, CAMERA_REQUEST); 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    if (resultCode == RESULT_OK) { 
     if (requestCode == CAMERA_REQUEST) 
     { 
      Bitmap photo = (Bitmap) data.getExtras().get("data"); 
      ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
      photo.compress(Bitmap.CompressFormat.JPEG, 40, bytes); 
      Random randomGenerator = new Random();randomGenerator.nextInt(); 
      String newimagename=randomGenerator.toString()+".jpg"; 
      File f = new File(Environment.getExternalStorageDirectory() 
            + File.separator + newimagename); 
      try { 
       f.createNewFile(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      //write the bytes in file 

      try { 
       fo = new FileOutputStream(f.getAbsoluteFile()); 
      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      try { 
       fo.write(bytes.toByteArray()); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
       uri=f.getAbsolutePath(); 
    //this is the url that where you are saved the image 
     } 



    } 

使用data.getExtras()获得( “数据”)。