Android Camera Intent无法正常工作-而是启动图库 [英] Android Camera Intent not working - Launching gallery instead

查看:147
本文介绍了Android Camera Intent无法正常工作-而是启动图库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过意图启动Android camaera,拍照并返回了URI.应该很简单,毕竟在SO上有很多关于如何执行此操作的帖子.

I am trying to launch the Android camaera via an intent, take a picture and have returned the URI. Should be quite simple right, after all there are loads and loads of posts on how to do this on SO.

但是,我正在使用带有ICS的星系S3,并且发现当我调用摄影机意图时,它会将我带到图库而不是摄影机.

However, I am using a galaxy S3 with ICS, and find that when I call the camera intent it takes me to the gallery not the camera.

这是我尝试过的代码.

首先是一个简单的

public static final int REQUEST_CODE_CAMERA = 1337;
.
.
. 
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(intent, REQUEST_CODE_CAMERA); 

这不起作用.因此,我尝试了 Vogella.com

This did not work. SoI tried the example from Vogella.com

private static final int REQUEST_CODE = 1;
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, REQUEST_CODE);

我也读过有人建议使用不同的请求代码,例如1888、1337和2500.我是否可以使用基于通用请求代码或基于框架的静态int?还是这些代码是许多SO鲱鱼中的一种,因此可以使用任何代码吗?

I have also read people suggesting different request codes such as 1888, 1337 and 2500. Is there not a universal request code or framework based static int I can use? Or are these code one of the many SO red herrings, and therefore one can use any code?

顺便说一句,我知道S3无法与MediaStore.EXTRA_OUTPUT一起使用的错误.这对我没有帮助.我已经知道如何克服这一障碍.

BTW I am aware of the bug in which the S3 does not work with the MediaStore.EXTRA_OUTPUT. It's not help with this that I need. I already know how to overcome that hurdle.

* *请注意

对于那些阅读此方向的人来说,这可能是一个严重的问题,在使用Camera Intent时需要注意.尽管启动相机的活动具有锁定在清单中的方向.当设备处于纵向模式时,它将在 OnActivityResult 之前和之后调用 OnCreate ;因此,将擦除类全局变量.解决方案是通过* onSaveInstanceStat * e方法保存所有类的全局变量,然后在 OnCreate 中使用它们在视图中重新加载并显示图像.从SO收集的信息中,并非所有设备都可以做到这一点,但是每个设备趋向于保持一致.我的猜测是活动的内存管理取决于可用的硬件和Android平台.我真的希望不是这样,但是Android是Android.

For those reading this orientation can be a serious problem that needs to be taken care of when using the Camera intent. Although the activity launching the camera has it's orientation locked in the manifest. When the device was in portrait mode it would call OnCreate before and after OnActivityResult; thus class global variables are wiped. The solution was to save all the class global variables via the *onSaveInstanceStat*e method, and in the OnCreate use these to reload and display the images in the view. From the information gleaned from SO not all devices will do this but it tends to be consistent per device. My guess is memory management of activities is dependant on available hardware and Android platform. I really wish this was not the case, but Android is Android.

[/EDIT]

推荐答案

MainActivity.java

MainActivity.java

public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 1888; 
private ImageView imageView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.imageView = (ImageView)this.findViewById(R.id.imageView1);
    Button photoButton = (Button) this.findViewById(R.id.button1);
    photoButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
            startActivityForResult(cameraIntent, CAMERA_REQUEST); 
            File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyCameraImages");
            imagesFolder.mkdirs();   
            File image = new File(imagesFolder, "image.jpg");
            Uri uriSavedImage = Uri.fromFile(image);
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);

        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        imageView.setImageBitmap(photo);        
} 
}
}

activity_main.xml

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_marginBottom="79dp" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="26dp"
    android:text="StartCamera" />

</RelativeLayout>

图库中保存的图像的快照

Snap Shot of image saved in Gallery

这篇关于Android Camera Intent无法正常工作-而是启动图库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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