Android摄像头OnActivityResult重置光路为n

Android摄像头OnActivityResult重置光路为n

本文介绍了Android摄像头OnActivityResult重置光路为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,同时测试在一个朋友的Galaxy S4(GT i9505的Andr​​oid 5.1),我的应用程序。如果给一个文件URI相机意图,OnActivityResult给出结果 Activity.RESULT_OK 键,并且该路径是。它正在对大多数其他设备我测试(LG G3,承上启下5 ...)。这是我的code:

GetOutputMediaFile

 公开文件getOutputMediaFile(整型){
    文件mediaStorageDir =新的文件(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),MediaChooserConstants.folderName);
    如果(!mediaStorageDir.exists()){
        如果(!mediaStorageDir.mkdirs()){
            返回null;
        }
    }

    //创建一个媒体文件名
    字符串的timeStamp =新的SimpleDateFormat(YYYYMMDD_HHMMSS)格式(新的Date())。
    文件媒体文件;
    如果(类型== MediaChooserConstants.MEDIA_TYPE_IMAGE){
        媒体文件=新的文件(mediaStorageDir.getPath()+文件分割符+IMG_+的timeStamp +.JPG);
    }否则,如果(类型== MediaChooserConstants.MEDIA_TYPE_VIDEO){
        媒体文件=新的文件(mediaStorageDir.getPath()+文件分割符+VID_+的timeStamp +.MP4);
    } 其他 {
        返回null;
    }
    返回媒体文件;
}
 

OnActivityResult

  @覆盖
公共无效onActivityResult(INT申请code,INT结果code,意图数据){
    如果(结果code == Activity.RESULT_OK){
        字符串picturePath = NULL;
        如果(要求code == MediaChooserConstants.CAPTURE_IMAGE_ACTIVITY_REQUEST_ code){
            picturePath = fileUri.getPath(); //< --- fileURI且空
        }
    }
}
 

DispatchTakePhotoIntent

 私人无效dispatchTakePictureIntent(){
    意向意图=新的意图(MediaStore.ACTION_IM​​AGE_CAPTURE);
    文件fil​​e = Utils.getInstance()getOutputMediaFile(MediaChooserConstants.MEDIA_TYPE_IMAGE)。

    =了fileURI Uri.fromFile(文件); //创建一个文件来保存图像
    intent.putExtra(MediaStore.EXTRA_OUTPUT,了fileURI); //设置图像文件名

    // fileURI且这里没有空,而调试(../DCIM/.../IMG_XXX.jpg)
    startActivityForResult(意向,MediaChooserConstants.CAPTURE_IMAGE_ACTIVITY_REQUEST_ code);

}
 

解决方案

图像像这样保存路径:

  @覆盖
保护无效的onSaveInstanceState(包savedInstanceState){

    如果(mImageCaptureUri!= NULL)
        savedInstanceState.putString(camera_image,mImageCaptureUri.toString());

    super.onSaveInstanceState(savedInstanceState);
}
 

和检索这个图片路径:

  @覆盖
保护无效onRestoreInstanceState(包savedInstanceState){

    如果(savedInstanceState!= NULL){
        如果(savedInstanceState.containsKey(camera_image)){
            mImageCaptureUri = Uri.parse(savedInstanceState.getString(camera_image));
        }
    }

    super.onRestoreInstanceState(savedInstanceState);
}
 

这个问题发生,只有当用户进入到相机的意图和的时候,他捕捉到的图像上摄像头的意图是托管被破坏或重新创建用户时,来自摄像头的意图后面的活动。

I'm having a problem while testing my app on a friends Galaxy S4 (GT i9505, Android 5.1). When giving a file URI to camera intent, OnActivityResult gives result Activity.RESULT_OK and and the path is null. It is working on most of other devices I tested (LG G3, nexus 5...). This is my code:

GetOutputMediaFile

public File getOutputMediaFile(int type){
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), MediaChooserConstants.folderName);
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            return null;
        }
    }

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

OnActivityResult

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        String picturePath = null;
        if (requestCode == MediaChooserConstants.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
            picturePath = fileUri.getPath(); // <--- fileURI is null
        }
    }
}

DispatchTakePhotoIntent

private void dispatchTakePictureIntent() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File file = Utils.getInstance().getOutputMediaFile(MediaChooserConstants.MEDIA_TYPE_IMAGE);

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

    //fileUri is not null here while debugging (../DCIM/.../IMG_XXX.jpg)
    startActivityForResult(intent, MediaChooserConstants.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

}
解决方案

Save path of image like this :

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {

    if(mImageCaptureUri!=null)
        savedInstanceState.putString("camera_image", mImageCaptureUri.toString());

    super.onSaveInstanceState(savedInstanceState);
}

And Retrieve image path from this :

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {

    if (savedInstanceState != null) {
        if (savedInstanceState.containsKey("camera_image")) {
            mImageCaptureUri = Uri.parse(savedInstanceState.getString("camera_image"));
        }
    }

    super.onRestoreInstanceState(savedInstanceState);
}

This problem happens, Only when user goes to camera intent and by the time he captures the image the activity on which camera intent was hosted gets destroyed or recreated when user comes back from the camera intent.

这篇关于Android摄像头OnActivityResult重置光路为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:30