gpt4 book ai didi

android - 未能将 Intent 结果传递回 Activity ,android 图像捕获 Intent

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

我正在尝试在我的 Android 应用程序中实现一个简单的图像捕获,并返回保存图像的路径。我正在关注 android 文档中的示例,但是当我捕获图像并按“保存”时,我的应用程序崩溃并出现以下异常:

Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.fideli/com.fideli.MainActivity}: java.lang.NullPointerException

为什么数据=空??我试图寻找修复方法,但没有一个有帮助。有人可以帮我提出建议吗?

谢谢!

主要 Activity :

  private void takePicture() {
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

}


/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}

/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.

File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.

// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}

// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
} else {
return null;
}

return mediaFile;
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//Bundle bundle = data.getExtras();
//Log.i("TAG", "Image saved to:\n" + bundle);
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
// Toast.makeText(this, "Image saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();
Log.i("TAG", "Image saved to:\n" + data.getData());
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
}

最佳答案

图像被写入您在 Intent extra MediaStore.ACTION_IMAGE_CAPTURE.EXTRA_OUTPUT 中传递的 Uri。在 onActivityResult() 中传递回您的 Activity 的 Intent 不包含图像,也不包含 data 中的 Uri Intent 的字段。在 onActivityResult() 中,您可以从调用 getOutputMediaFileUri(int type) 时返回的文件中获取图像。

关于android - 未能将 Intent 结果传递回 Activity ,android 图像捕获 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26044021/

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