三星I9300手机在调用系统摄像头拍照onActivityResult data为null的解决方法

程序需求,需要上传头像,所以就调用了三星盖三的系统拍照方法

调用系统拍照方法如下:


// 调用系统拍照程序自定义保存文件路径
private void takePhoto() {
	Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
	Uri uri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
	intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); // set the image file name
	startActivityForResult(intent, Contants.TAKEPHOTO);
}


MediaStore.ACTION_IMAGE_CAPTURE - Intent Action type to requesting an image from an existing camera application

MediaStore. EXTRA_OUTPUT - 这个设置需要一个Uri对象指定一个路径和文件名,您想保存图片。这个设置是可选的,但是强烈建议。如果不指定这个值,相机应用程序将请求的图片保存在默认位置缺省名称,指定返回的意图的Intent.getData()。


	// 生成图片URI
	public final int MEDIA_TYPE_IMAGE = 1;
	public final int MEDIA_TYPE_VIDEO = 2;

	private Uri getOutputMediaFileUri(int type) {
		return Uri.fromFile(getOutputMediaFile(type));
	}

	/** Create a File for saving an image or video */
	private File getOutputMediaFile(int type) {
		// To be safe, you should check that the SDCard is mounted
		// using Environment.getExternalStorageState() before doing this.
		File mediaStorageDir = null;
		if (MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
			mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "image");
		} else {
			mediaStorageDir = new File(getFilesDir(), "image");
		}
		// File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
		// Environment.DIRECTORY_PICTURES), "tfm");
		// 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", Locale.CHINA).format(new Date());
		File mediaFile;
		if (type == MEDIA_TYPE_IMAGE) {
			mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
		} else if (type == MEDIA_TYPE_VIDEO) {
			mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");
		} else {
			return null;
		}
		return mediaFile;
	}

@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == Contants.TAKEPHOTO) { // 拍照
			if (resultCode == Activity.RESULT_OK) {
				if(data != null) {
					Uri fileUri = data.getData();				
					// 处理图片
				}
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

上面就是调用拍照,返回照片处理的全过程了,但是在测试时,出现了data为null的情况,查看Activity生命周期,发现在调用系统拍照后Activity被onDestroy了,然后onCreate.

这是三星手机拍照横竖屏切换照成的。

解决方法:

在Activity的配置清单中,我们指定android:configChanges="keyboardHidden|orientation"然后重写onConfigurationChanged方法即可

考虑到程序在配置时使用了minSdkVersion或者targetSdkVersion有大于或者等于13的可能,

这个时候在使用android:configChanges="keyboardHidden|orientation",那么onConfigurationChanged方法是不会被调用的,我们需要指定screensize这个属性

android:configChanges="keyboardHidden|orientation|screensize"才能满足我们的需要


	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		Log.i("UserInfoActivity", "onConfigurationChanged");
		if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
			Log.i("UserInfoActivity", "横屏");
			Configuration o = newConfig;
			o.orientation = Configuration.ORIENTATION_PORTRAIT;
			newConfig.setTo(o);
		} else if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
			Log.i("UserInfoActivity", "竖屏");
		}
		super.onConfigurationChanged(newConfig);
	}

你可能感兴趣的:(Android高级)