0

在我的应用程序中,我试图让用户可以按下一个按钮,这将允许他们使用手机上的相机应用程序拍照。

我正在按照 Android 开发人员指南 (http://developer.android.com/guide/topics/media/camera.html#intent-收到)

我在使用 onActivityResult() 方法时遇到问题,它显然需要 3 个参数

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if(resultCode == RESULT_OK) {
            Log.w("borre","Image saved to:\n" + data.getData());
        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the image capture
        } else {
            // Image capture failed, advise user
        }
    }
}

但目前,数据 Intent 以 null 形式返回,因此在 Intent 参数上调用任何方法都会引发 NullPointerException

这是我用来调用相机应用程序的代码(它与指南中的代码基本相同)

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

有没有人遇到过这个问题或知道为什么这个 Intent 会返回为空?

4

2 回答 2

4

intent.setDataAndType()当您启动 Acitivty.like 时,您正在获取数据部分 null bez 您没有设置

    public static final String IMAGE_UNSPECIFIED = "image/*"; 
    Intent intent = new Intent(Intent.ACTION_PICK, null);  
    intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED);  
    startActivityForResult(intent, 3);  

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == 0)  
       return; 
    if (requestCode == 2) {  
       Uri uri=data.getData();  //YOU GET DATA HERE
        }  
//OR
if (requestCode == 3) {  
 Bundle extras = data.getExtras();  
 if (extras != null) {  
 Bitmap photo = extras.getParcelable("data");  
 ByteArrayOutputStream stream = new ByteArrayOutputStream();  
 photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0 - 100)????  
 imageView.setImageBitmap(photo);  
  }  
}  

或者在您的情况下获取图像路径使用:

if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {  
     //pic path
      File picture = new File(Environment.getExternalStorageDirectory() + "/temp.jpg");  
   } 
于 2012-04-07T17:03:19.550 回答
0

在类范围内声明 imageUri = null ...

使用下面的代码并尝试::

takephoto.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Calendar cal = Calendar.getInstance();
        File file = new File(Environment.getExternalStorageDirectory(),(cal.getTimeInMillis()+".jpg"));
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            file.delete();
            try {
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        imageUri = Uri.fromFile(file);
        Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        i.putExtra(MediaStore.EXTRA_OUTPUT, selectedImageUri);
        startActivityForResult(i, 0);
    }
});

这里也是 onactivityresult 方法:::

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case 0:
            if (resultCode == RESULT_OK) {
                try {
                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), imageUri);
                    //imageVIew.setImageBitmap(bitmap);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            break;
    }
}
于 2012-04-07T16:53:47.700 回答