从相机中选择图像时无法交付结果,但从图库中选择图像时没有问题
Posted
技术标签:
【中文标题】从相机中选择图像时无法交付结果,但从图库中选择图像时没有问题【英文标题】:Failure delivering result while picking image from camera but no issue when picking image from gallery 【发布时间】:2017-11-30 06:11:28 【问题描述】:我正在开发一个应用程序,其中有一个按钮,用于从图库中选择图像或从相机捕获图像。当我从图库中选择图像时,最终没有错误图像显示正常,但是当我从相机应用程序捕获图像时崩溃。
以下是错误:
java.lang.RuntimeException: Failure delivering result ResultInfowho=null, request=66, result=-1, data=null to activity
com.byteshaft.prospectform/com.byteshaft.prospectform.MainActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:4297)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4347)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1557)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:173)
at android.app.ActivityThread.main(ActivityThread.java:6459)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:938)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference
at com.byteshaft.prospectform.MainActivity.onActivityResult(MainActivity.java:400)
at android.app.Activity.dispatchActivityResult(Activity.java:6926)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4293)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4347)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1557)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:173)
at android.app.ActivityThread.main(ActivityThread.java:6459)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:938)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)
我从画廊/相机中挑选图像的代码:
public void openImageIntent()
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String fname = "Prospect-form" + timeStamp;
final File sdImageMainDirectory = new File(storageDir, fname);
outputFileUri = Uri.fromFile(sdImageMainDirectory);
// Camera.
final List<Intent> cameraIntents = new ArrayList<>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for(ResolveInfo res : listCam)
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
//Gallery.
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Filesystem.
final Intent fsIntent = new Intent();
fsIntent.setType("*/*");
fsIntent.setAction(Intent.ACTION_GET_CONTENT);
cameraIntents.add(fsIntent);
//Create the Chooser
final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
startActivityForResult(chooserIntent, 66);
onActivityResult 代码:`
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if(resultCode == Activity.RESULT_OK)
switch (requestCode)
case 5:
Uri selectedImage = data.getData();
try
Bitmap bitmap = MediaStore.Images.Media.getBitmap(MainActivity.this.getContentResolver(), selectedImage);
Bitmap resizedBitMap;
resizedBitMap = Bitmap.createScaledBitmap(bitmap, 100, 100, true);
imageView.setImageBitmap(resizedBitMap);
System.out.println(Helpers.getPath(MainActivity.this, selectedImage));
imagePath = Helpers.getPath(MainActivity.this, selectedImage);
catch (IOException e)
Log.i("TAG", "Some exception " + e);
break;
`
*** 上有一些非常有用的答案,但我无法解决我的问题。只有当我从相机中选择捕获图像时才会出现错误,否则当我从图库中选择图像时根本没有错误。
有什么建议吗?
【问题讨论】:
【参考方案1】:发生这种情况是因为从相机和图库中选择图像不会返回相同的结果。您必须通过查看意图来检查用户选择了哪一个。你可以这样做:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if (requestCode == mRequestCode)
if (resultCode == Activity.RESULT_OK)
boolean isCamera = true;
if (data != null && data.getData() != null)
String action = data.getAction();
isCamera = MediaStore.ACTION_IMAGE_CAPTURE.equals(action);
try
Uri uriFileSrc = isCamera ? mOutputFileUri : data.getData();
//Do what do you need with the Uri
catch (Exception ex)
Toast.makeText(mActivity, R.string.error_creating_file, Toast.LENGTH_SHORT).show();
【讨论】:
以上是关于从相机中选择图像时无法交付结果,但从图库中选择图像时没有问题的主要内容,如果未能解决你的问题,请参考以下文章