onActivityresult数据为空 [英] onActivityresult data is null
本文介绍了onActivityresult数据为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的相机应用程序,我想在其中捕获图像并对其进行裁剪,但是它拍摄的照片保存在我的"myimage"目录中,但不执行裁剪功能.场地 这是我的相机开源代码
this is my camera app in which i want to capture image and crop it but it take picture is save in my ""myimage" directory but does't perform the crop functionality. please i need a help i am an new this field this is my camera open source code
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/MyImage");
file.mkdir();
String timestmp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File images = new File(file, "QR_" + timestmp + ".jpg");
Urisavedmsg=Uri.fromFile(images);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Urisavedmsg);
startActivityForResult(intent, REQUEST_Code);
这是我的活动"结果代码,此处执行的其他任何操作
if (requestCode == REQUEST_Code && resultCode==Activity.RESULT_OK )
if (Urisavedmsg!=null)
{
File file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/Myimage");
if (Urisavedmsg!=null){
try {
Uri getImage;
getImage=data.getData();
cropCapturedImage(Uri.fromFile(file));
cropCapturedImage(getImage);
} catch (ActivityNotFoundException ex) {
String msg = "sorry your device does't support the crop the action!";
Toast toast = Toast.makeText(this, msg, Toast.LENGTH_LONG);
toast.show();
}}
if (requestCode == 2) {
Bundle bundle = data.getExtras();
Bitmap thepic = bundle.getParcelable("data");
mImageView.setImageBitmap(thepic);
}
} else {
Toast.makeText(getApplicationContext(), "some thing worng", Toast.LENGTH_LONG).show();
}
}
这是我的裁剪方法.....
private void cropCapturedImage(Uri picUri) {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(picUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
cropIntent.putExtra("scale", true);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, Crop_pic);
}
推荐答案
尝试下面的代码,它对我来说在片段中正常工作,如果您在Activity类中执行操作,则相应地更改代码.
Try below code, it works for me properly in fragment, change code accordingly if you are performing action in Activity class.
File file = new File(Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/picture").getPath());
if (!file.exists()) {
file.mkdirs();
}
capturedImageUri = Uri.fromFile(File.createTempFile("myImages" + new SimpleDateFormat("ddMMyyHHmmss", Locale.US).format(new Date()), ".jpg", file));
intent.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri);
startActivityForResult(intent, Util.REQUEST_CAMERA);
现在onActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == getActivity().RESULT_OK) {
switch (requestCode) {
case Util.REQUEST_CAMERA:
try {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) && !Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED_READ_ONLY)) {
File file = new File(Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+ "./myImages"+ File.separator+ "picture").getPath());
if (!file.exists()) {
file.mkdirs();
}
selectedPath1 = File.createTempFile("myImages"+ new SimpleDateFormat("ddMMyyHHmmss",Locale.US).format(new Date()),".jpg", file).toString();
croppedImageUri = Uri.fromFile(new File(selectedPath1));
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(capturedImageUri, "image/*");
intent.putExtra("outputX", 400);
intent.putExtra("outputY", 400);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("noFaceDetection", true);
intent.putExtra("output", croppedImageUri);
startActivityForResult(intent, Util.REQUEST_CROP_IMAGE);
} else {
Toast.show(getActivity(), "Please insert memory card to take pictures and make sure it is write able");
}
} catch (Exception e) {
e.printStackTrace();
}
break;
case Util.REQUEST_CROP_IMAGE:
Logg.e(getClass().getSimpleName(), "Profile_Pic ===== " + selectedPath1);
imgProfile.setImageURI(Uri.parse("file://" + croppedImageUri));
break;
default:
break;
}
}
}
这篇关于onActivityresult数据为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文