Android Lollipop 问题 - 无法将图像从相机加载到 ImageView

Android Lollipop issue - Cannot load image from camera to ImageView(Android Lollipop 问题 - 无法将图像从相机加载到 ImageView)

本文介绍了Android Lollipop 问题 - 无法将图像从相机加载到 ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 android lollipop 之前的任何版本上,以下代码都可以正常工作.出于某种原因,从某个版本的android(大约5.0)开始,每当从相机捕获图像时,屏幕都会向右和向后旋转90度(不仅我的设备上的自动旋转关闭,我的活动被定义为肖像,它根本不应该旋转!).一旦屏幕旋转回来,ImageView 就会显示上一个(原始)图像.有什么建议吗?

On any version before android lollipop, the below code works fine. For some reason, from a certain version of android (around 5.0), whenever an image is captured from camera, the screen rotates 90 degrees to the right and back (Not only auto-rotate on my device is off, my activity is defined as portrait, it's not supposed to be rotating at all!). Once the screen rotates back, the ImageView presents the previous (original) image. Any suggestions?

相机意图:

if (result.equals("CAMERA"))
{
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, RESULT_IMAGE_CAPTURE);
}

实际操作:

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);

    Bitmap bmp = null;

    if (requestCode == RESULT_IMAGE_CAPTURE && resultCode == RESULT_OK && data != null)
        Bitmap bmp = (Bitmap) data.getExtras().get("data");

    if (bmp != null)
    {
        mProfilePicPath = ImageHandler.saveBitmap(bmp , "", "image_name");
        mProfilePic.setImageBitmap(bmp);
    }
}   

编辑:显然,当从相机意图返回我的活动时,不仅仅是要调用的 onResume() 方法,而是调用 onCreate() 方法,而且不仅仅是一次,而是两次!第一次没有问题,因为它之后调用了onActivityResult方法.但是,第二次重新启动 mProfilePic(我的 ImageView)和 mProfilePicPath,我想稍后使用它们.有什么想法吗?

EDIT: Apparently when going back to my activity from the camera intent, rather than just the onResume() method to be called, the onCreate() method is called, and not just once, but twice! The first time is not a problem, since the onActivityResult method called after it. The second time, though, re-initiates both mProfilePic (my ImageView) and mProfilePicPath, which I want to use later. Any ideas?

推荐答案

好的,显然这就是问题(和解决方案) -通过意图拍照后调用的活动终止/onCreate

OK, apparently this was the problem (and the solution) - Activity killed / onCreate called after taking picture via intent

我的清单中没有该行(尽管我确实将我的活动定义为肖像):android:configChanges="orientation|keyboardHidden|screenSize"

I didn't have that line in my manifest (though i did define my activity as portrait): android:configChanges="orientation|keyboardHidden|screenSize"

这篇关于Android Lollipop 问题 - 无法将图像从相机加载到 ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android Lollipop 问题 - 无法将图像从相机加载到 ImageView

基础教程推荐