API级别> = 23相机意图上的空URI [英] Null URI on API level >=23 camera intent
问题描述
这可能是重复的,但我似乎找不到确切的解决方案。
摄像头工作正常,并在api 23以下的设备上返回图像uri。
但是,应用程序在apove api 23上的设备上捕获图像并出现日志错误后立即崩溃
This might be a possible duplicate but i can't seem to pinpoint the exact solution. Camera works fine and returns the image uri on devices below api 23. However app crushes right after capturing image on devices apove api 23 with log error
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { act=inline-data (has extras) }} to activity {ana.foodi/ana.foodi.MainActivity}: java.lang.NullPointerException: uri
at android.app.ActivityThread.deliverResults(ActivityThread.java:3940)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3983)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1548)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5765)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Caused by: java.lang.NullPointerException: uri
at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:60)
at android.content.ContentResolver.query(ContentResolver.java:481)
at android.content.ContentResolver.query(ContentResolver.java:441)
at ana.foodi.MainActivity.onActivityResult(MainActivity.java:672)
经断点检查后,实现的Uri(selectedImage)在下面为空:
After checking with breakpoints,realised Uri (selectedImage) is null below:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
final String filePath;
if(resultCode == Activity.RESULT_OK && REQUEST_POST_PHOTO==999){
if(requestCode == 2 ) {
requestRuntimePermission();
Uri selectedImage = data.getData();
String[] filePaths = {MediaStore.Images.Media.DATA};
Cursor c = this.getContentResolver().query(selectedImage, filePaths, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePaths[0]);
filePath = c.getString(columnIndex);
c.close();
我尝试使用此
public void requestRuntimePermission() {
if (Build.VERSION.SDK_INT >= 23) {
if (ContextCompat.checkSelfPermission(context,android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
}
}
但错误仍然存在。
startActivityForResult调用
startActivityForResult call
REQUEST_POST_PHOTO=999;
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, REQUEST_CODE_POST);
推荐答案
您正在使用 ACTION_IMAGE_CAPTURE
。 ACTION_IMAGE_CAPTURE
不应通过 onActivityResult()
Uri >。相反,要么:
You are using ACTION_IMAGE_CAPTURE
. ACTION_IMAGE_CAPTURE
is not supposed to return a Uri
via onActivityResult()
. Instead, either:
-
您在<$ c上提供
EXTRA_OUTPUT
$ c> ACTION_IMAGE_CAPTURE意图
,在这种情况下,图像应保存到该位置(请参见此示例应用),或
You provide
EXTRA_OUTPUT
on theACTION_IMAGE_CAPTURE
Intent
, in which case the image is supposed to be saved to that location (see this sample app), or
您未在 ACTION_IMAGE_CAPTURE
意图$ c>上提供
传递到 EXTRA_OUTPUT
$ c>,在这种情况下,您会通过数据
在$$ c上获得缩略图位图
$ c>意图 onActivityResult()
You do not provide EXTRA_OUTPUT
on the ACTION_IMAGE_CAPTURE
Intent
, in which case you get a thumbnail Bitmap
back via the "data"
extra on the Intent
delivered to onActivityResult()
文档对此进行了介绍。
某些越野车相机应用 会返回 Uri
。不要指望这种行为,因为编写精良的相机应用程序不会。
Some buggy camera apps will return a Uri
. Do not count on this behavior, as well-written camera apps will not.
这篇关于API级别> = 23相机意图上的空URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!