天天看点

Android中调用本机camera,遇到无法回调onActivityresult方法

今天写项目调用本机camera时,遇到无法回调onActivityresult方法,调用本机camera的代码如下:

bt1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (openCamera) {
					Intent openCameraIntent = new Intent(
							android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
					File file = new File(Environment
							.getExternalStorageDirectory().getAbsolutePath()
							+ "/myimage/", String.valueOf(System
							.currentTimeMillis()) + ".jpg");
//					if (!file.exists())         
//					    file.getParentFile().mkdirs();
					path = file.getPath();

					Uri imageUri = Uri.fromFile(file);
					openCameraIntent.putExtra(
							MediaStore.Images.Media.ORIENTATION, 0);
					// 下面这句指定调用相机拍照后的照片存储的路径
					openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
					startActivityForResult(openCameraIntent, 1);
					openCamera = false;
				}
				popupWindow.dismiss();
			}
		});
           

第一次写的如上,是没有被注释的那行的,因为这个文件夹在Android手机中是没有的,系统无法自动找到路径,所以造成data的数据可能是空的,所以无法回调,所以解决方法就是加上被注释的这句:

if (!file.exists())         
   file.getParentFile().mkdirs();
           

耽误了一点时间,mark一下,高手勿喷