gpt4 book ai didi

android - 从相机中选取图像时无法提供结果,但从图库中选取图像时没有问题

转载 作者:行者123 更新时间:2023-11-30 00:29:11 24 4
gpt4 key购买 nike

我正在开发一个应用程序,其中有一个按钮,用于从图库中选择图像或从相机捕获图像。当我从图库中选择图像时,最终没有错误图像正常显示,但是当我从我的相机应用程序中捕获图像时崩溃。

错误如下:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=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;
}
}`

stackoverflow 上有一些非常有用的答案,但我无法解决我的问题。只有当我选择从相机捕获图像时才会出现错误,否则当我从图库中选择图像时根本没有错误。

有什么建议吗?

最佳答案

发生这种情况是因为从相机和图库中选取图像不会返回相同的结果。您必须通过查看 Intent 来检查用户选择了哪一个。你可以这样做:

@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();
}
}
}
}

关于android - 从相机中选取图像时无法提供结果,但从图库中选取图像时没有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44776055/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com