我的 Android 相机 Uri 返回一个空值,但三星修复程序已经到位,帮助? [英] My Android camera Uri is returning a null value, but the Samsung fix is in place, help?

查看:9
本文介绍了我的 Android 相机 Uri 返回一个空值,但三星修复程序已经到位,帮助?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我知道三星设备上的相机问题.您需要在调用相机意图之前创建一个 Uri,如下所示:

So I am aware of the camera's issue on Samsung devices. You need to create a Uri before calling the camera intent like so:

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, m_username);
mImageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

此示例为相机创建一个 Uri 来存储数据.我的应用在两个位置都有相机功能.两者是相同的(直接复制粘贴),但在一个活动中,mImageUri 对象在相机过程中变为空,而在另一个活动中则没有.后者工作正常,但前者给出一个空值,这反过来又阻止我保存图像、预览图像等.我将在下面提供 onResult 代码:

This example creates a Uri for the camera to store the data in. My app has a camera function in two locations. Both are identical (direct copy paste), but in one activity the mImageUri object becomes null during the camera process, while in the other it doesn't. The latter works fine, but the former gives a null value which in turn stops me from saving the image, previewing it, etc. I will provide the onResult code below:

case CAMERA_PIC_REQUEST :
if(resultCode == RESULT_OK) {
    if(intent != null) {
        //For non-Samsung devices
        Log.e("Intent value:", intent.toString());
        mImageUri = intent.getData();
    }

    mAvatar = BitmapStatic.createImage(mImageUri, this);
    Drawable draw = new BitmapDrawable(getResources(), mAvatar);
    m_photoButtonE.setImageDrawable(draw);
    m_imageChanged = true;
}
break;

上面的例子来自工作活动.以下是不起作用的活动:

The above example is from the working activity. Below is the activity where this doesn't work:

//On pressing the "Take Photo" button:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, mUsername);
mImageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
if(mImageUri == null) {
    Log.e("image uri is null", "what?");
}
else {
    Log.e("oh nevermind", "image uri is NOT null");
}
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

//On Result:
case CAMERA_PIC_REQUEST :
if(resultCode == RESULT_OK) {
    if(intent != null) {
        Log.e("Intent value:", intent.toString());
        mImageUri = intent.getData();
    }
    else {
        Log.e("Intent is null", "yep it is.");
        if(mImageUri == null) {
            Log.e("nullcheck on memberimageuri", "its null");
        }
        else {
            Log.e("nullcheckon memberimage", mImageUri.toString());
        }
    }
    mImage = BitmapStatic.createImage(mImageUri, this);
    Drawable draw = new BitmapDrawable(getResources(), mImage);
    mPhotoPreview.setImageDrawable(draw);
}
break;

请注意我在第二个示例中放置的日志文件.当我单击拍照"按钮时,mImageUri 对象的空检查返回 true.所以这个对象不为空.但是在拍照过程中的某个地方,该值确实变为空.onResult() 期间的第二次检查返回 false.同样,这与我在其他活动中的设置几乎相同,它工作正常.

Notice the log files I have put in this 2nd example. When I click the "Take Picture" button, the null check for the mImageUri object returns true. So this object is not null. But somewhere during the picture taking process that value DOES become null. The 2nd check during onResult() returns false. Again, this is almost identical to my setup in the other activity, where it works fine.

有什么想法吗?

推荐答案

您的 Activity 在 Camera Activity 操作期间被销毁,之后又重新创建.你应该使用 onSaveInstanceState/onRestoreInstanceState 机制在您的活动中在活动重新启动时保留图像 URI(和任何其他数据).

Your activity gets destroyed during the Camera activity operation and re-created afterwards. You should use onSaveInstanceState/onRestoreInstanceState mechanism in your activity to preserve the image URI (and any other data) upon the activity restarts.

像这样:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    if (mImageUri != null) {
        outState.putString("cameraImageUri", mImageUri.toString());
    }
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    if (savedInstanceState.containsKey("cameraImageUri")) {
        mImageUri = Uri.parse(savedInstanceState.getString("cameraImageUri"));
    }
}

这篇关于我的 Android 相机 Uri 返回一个空值,但三星修复程序已经到位,帮助?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆