在我的应用中,我必须从原生相机应用中捕获照片,然后将其存储在我想要的文件夹中,然后在新的活动中显示捕获的图像。我的代码到目前为止
启动原生相机
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);
我的其他方法
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
/**Create a file for saving an image*/
private static File getOutputMediaFile(int type){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "KidzPuzzle");
//Create a storage directory if it does not exist
if(! mediaStorageDir.exists()){
if(! mediaStorageDir.mkdirs()){
Log.d("KidzPuzzle", "Failed to create directory");
return null;
}
}
//Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile = null;
if(type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath()+File.separator+"IMG_"+timeStamp+".jpg" );
return mediaFile;
}else{
return null;
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE){
if(resultCode == RESULT_OK){
//Image created and saved successfully to the specified fileUri
//Toast.makeText(this, "Image saved to : \n"+data.getDataString(), Toast.LENGTH_LONG).show();
String filePath = ((Uri) data.getExtras().get(MediaStore.EXTRA_OUTPUT)).toString();
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
startActivity(new Intent(this, SplitImageActivity.class).putExtra("image", bitmap));
}else if(resultCode == RESULT_CANCELED){
//user canceled the captured image
}else{
//Image capture failed
}
}
}
onActivityResult中的intent变量 data 始终为null。在我出错的地方请帮助我。
注意:当我调试它时,由于数据的值,控件始终满足RESULT_OK条件并显示 NullPointerException 。
另一件事,如果我不使用
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
此代码在启动本机相机之前,数据不为空。
请帮帮我。
答案 0 :(得分:0)
Chandra Sekhar,
正如您在上一次提到的那样,当您don't specify location
为图像时,data is not null
所以AFAIK
它清楚地表明当您通过位置时它将返回null并且您将收到您所在位置的图片。因此,当您获得location separately and use that to access image
时,您需要存储RESULT_OK
。