Android 安卓拍摄相机返回空意图
我正在开发android应用程序,我的应用程序有拍照按钮。之前,我陷入了一种状态,在拍摄照片后,onActivityResult中返回的数据为null。这是相机预期的行为,如果我们将Android 安卓拍摄相机返回空意图,android,android-intent,camera,Android,Android Intent,Camera,我正在开发android应用程序,我的应用程序有拍照按钮。之前,我陷入了一种状态,在拍摄照片后,onActivityResult中返回的数据为null。这是相机预期的行为,如果我们将额外的\u输出设置为intent,它将返回null。出于这个原因,我对代码进行了空检查,结果很好 几天后,我再次进行了测试。我还是再次陷入同样的问题。但是这次数据不是nulldata具有空的意图,例如intent和data。getData()变为null。我通过检查data来修复此问题。getData()==null
额外的\u输出设置为intent,它将返回null
。出于这个原因,我对代码进行了空检查,结果很好
几天后,我再次进行了测试。我还是再次陷入同样的问题。但是这次数据
不是null
data
具有空的意图,例如intent
和data。getData()
变为null
。我通过检查data来修复此问题。getData()==null
再次工作。我不知道为什么会这样。只是好奇发生了什么。由于这个原因,我必须重新上传到生产-(
已编辑
我知道如何解决这个问题。我不明白的是,如果我输入额外的输出,返回的data
必须是null
。最重要的是,我几周前实现的代码,我非常确定data
返回null
,突然又变成了非null
值。不要使用data.getData()
,改为使用data.getExtras().get(“data”);
,查看问题是否再次出现。我不使用data.getExtras()的原因
是因为如果我在intent中传递额外的输出,数据
变成null
。如果我没有错的话,这就是事实。谢谢@Rohit的回答,但我看不出我的代码有什么不同。你能指出为什么intent
返回数据突然变成非null值吗?
Actually the camera intent doesnot return the data in intent because after getting image it kill the activity.
so try this
void opencameraForPicture(int requestCode, Uri fileUri) {
checkPermissionForMarshMello(Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE);
Intent intent = new Intent(Constants.CAMERA_INTERNAL_CLASS);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
/* start activity for result pass intent as argument and request code */
startActivityForResult(intent, requestCode);
}
/**
* This method set the path for the captured image from camera for adding
* the new picture in the list
*/
private Uri getOutputMediaFile() {
File mediaStorageDir = new File(
Environment.getExternalStorageDirectory(), "."
+ Constants.CONTAINER);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
mediaStorageDir.mkdirs();
}
File mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + System.currentTimeMillis() + ".png");
Uri uri = null;
if (mediaFile != null) {
uri = Uri.fromFile(mediaFile);
}
return uri;
}
in @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String imagePath = fileUri.getPath();
//you can decode this path as bitmap
}
Actually the camera intent doesnot return the data in intent because after getting image it kill the activity.
so try this
void opencameraForPicture(int requestCode, Uri fileUri) {
checkPermissionForMarshMello(Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE);
Intent intent = new Intent(Constants.CAMERA_INTERNAL_CLASS);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
/* start activity for result pass intent as argument and request code */
startActivityForResult(intent, requestCode);
}
/**
* This method set the path for the captured image from camera for adding
* the new picture in the list
*/
private Uri getOutputMediaFile() {
File mediaStorageDir = new File(
Environment.getExternalStorageDirectory(), "."
+ Constants.CONTAINER);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
mediaStorageDir.mkdirs();
}
File mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + System.currentTimeMillis() + ".png");
Uri uri = null;
if (mediaFile != null) {
uri = Uri.fromFile(mediaFile);
}
return uri;
}
in @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String imagePath = fileUri.getPath();
//you can decode this path as bitmap
}