对于某些手机,Android data.getData()从CameraActivity返回null [英] Android data.getData() returns null from CameraActivity for some phones
问题描述
我的onActivityResult中发生致命错误,原因是相机活动回来了.我挠头的是,该错误仅在少数几部手机上发生(基于受影响的用户数量),而大多数手机似乎都没错.我可以在Nexus 6(运行Lollipop 5.1.1)上复制该错误,而我的Note 5(也是5.1.1)完全没有问题.
I have a fatal error occurring in my onActivityResult coming back from a camera activity. What has me scratching my head is that the error is only happening on a handful of phones (based on the number of affected users) while there seems to be nothing wrong for the majority. I can duplicate the error on my Nexus 6 (running Lollipop 5.1.1) while my Note 5 (also 5.1.1) has no problems at all.
问题是当我尝试从data.getData()分配imageUri时.在Note 5上进行调试,data.mData等于"content://media/external/images/media/2215",而在Nexus 6上,data.mData为null.
The problem is when I am trying to assign the imageUri from data.getData(). Debugging on the Note 5, data.mData equals "content://media/external/images/media/2215" while on the Nexus 6, data.mData is null.
我知道这是一个关于SO的常见问题,但到目前为止,我还没有发现任何对我有帮助的东西.谁能指出我的解决方案并提供答案?
I know this is a common question asked on SO but I haven't found anything that has helped me so far. Can anyone point me to the solution for this and provide an answer?
为结果启动相机活动的方法
@OnClick(R.id.change_image_camera) public void takePicture(){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);}
onActvityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
Uri imageUri = data.getData(); //The trouble is here
String realPath = Image.getPath(this, imageUri); //getPath crashes because imageUri is null
Image.compressImage(realPath);
File file = new File(realPath);
Bundle extra = new Bundle();
extra.putString("URL", realPath);
returnIntent.putExtras(extra);
setResult(RESULT_OK, returnIntent);
finish();
}
}
非常感谢您对此提供的任何帮助!
I greatly appreciate any help on this one!
推荐答案
Uri imageUri = data.getData(); //The trouble is here
There is no requirement for a camera app to return a Uri
pointing to the photo, as that is not part of the ACTION_IMAGE_CAPTURE
Intent
protocol. Either:
-
在
ACTION_IMAGE_CAPTURE
Intent
中提供EXTRA_OUTPUT
,在这种情况下,您知道应该将图像存储在什么位置,而不必依赖getData()
或
Supply
EXTRA_OUTPUT
in yourACTION_IMAGE_CAPTURE
Intent
, in which case you know where the image should be stored, and do not need to rely upongetData()
, or
在响应Intent
中使用data
附加字符,它将是图像缩略图的Bitmap
Use the data
extra in the response Intent
, which will be a Bitmap
of a thumbnail of the image
您的下一个bug在这里:
Your next bug is here:
String realPath = Image.getPath(this, imageUri);
除非您通过EXTRA_OUTPUT
提供了到该位置的路径,否则无需将图像存储为可以访问的文件.
There is no requirement that the image be stored as a file that you can access, unless you provide a path to that location via EXTRA_OUTPUT
.
这篇关于对于某些手机,Android data.getData()从CameraActivity返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!