Android ACTION_IMAGE_CAPTURE 意图 [英] Android ACTION_IMAGE_CAPTURE Intent

查看:39
本文介绍了Android ACTION_IMAGE_CAPTURE 意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试使用原生相机应用让用户拍摄新照片.如果我们省略 EXTRA_OUTPUT extra 并返回小的位图图像,它就可以正常工作.但是,如果我们在启动 Intent 之前 putExtra(EXTRA_OUTPUT,...),则一切都会正常工作,直到您尝试点击相机应用程序中的确定"按钮.确定"按钮什么也不做.相机应用程序保持打开状态,没有任何锁定.我们可以取消它,但文件永远不会被写入.我们究竟需要做什么才能让 ACTION_IMAGE_CAPTURE 将拍摄的照片写入文件?

We are trying to use the native camera app to let the user take a new picture. It works just fine if we leave out the EXTRA_OUTPUT extra and returns the small Bitmap image. However, if we putExtra(EXTRA_OUTPUT,...) on the intent before starting it, everything works until you try to hit the "Ok" button in the camera app. The "Ok" button just does nothing. The camera app stays open and nothing locks up. We can cancel out of it, but the file never gets written. What exactly do we have to do to get ACTION_IMAGE_CAPTURE to write the picture taken to a file?

这是通过 MediaStore.ACTION_IMAGE_CAPTURE 意图完成的,只是为了清楚

This is done via the MediaStore.ACTION_IMAGE_CAPTURE intent, just to be clear

推荐答案

这是一个某些版本的 android 中记录良好的错误.也就是说,在 android 的谷歌体验版本中,图像捕获无法按照文档进行.我通常在实用程序类中使用的是这样的东西.

this is a well documented bug in some versions of android. that is, on google experience builds of android, image capture doesn't work as documented. what i've generally used is something like this in a utilities class.

public boolean hasImageCaptureBug() {

    // list of known devices that have the bug
    ArrayList<String> devices = new ArrayList<String>();
    devices.add("android-devphone1/dream_devphone/dream");
    devices.add("generic/sdk/generic");
    devices.add("vodafone/vfpioneer/sapphire");
    devices.add("tmobile/kila/dream");
    devices.add("verizon/voles/sholes");
    devices.add("google_ion/google_ion/sapphire");

    return devices.contains(android.os.Build.BRAND + "/" + android.os.Build.PRODUCT + "/"
            + android.os.Build.DEVICE);

}

然后当我启动图像捕获时,我创建了一个检查错误的意图.

then when i launch image capture, i create an intent that checks for the bug.

Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
if (hasImageCaptureBug()) {
    i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File("/sdcard/tmp")));
} else {
    i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
}
startActivityForResult(i, mRequestCode);

然后在我返回的活动中,我根据设备做不同的事情.

then in activity that i return to, i do different things based on the device.

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
     switch (requestCode) {
         case GlobalConstants.IMAGE_CAPTURE:
             Uri u;
             if (hasImageCaptureBug()) {
                 File fi = new File("/sdcard/tmp");
                 try {
                     u = Uri.parse(android.provider.MediaStore.Images.Media.insertImage(getContentResolver(), fi.getAbsolutePath(), null, null));
                     if (!fi.delete()) {
                         Log.i("logMarker", "Failed to delete " + fi);
                     }
                 } catch (FileNotFoundException e) {
                     e.printStackTrace();
                 }
             } else {
                u = intent.getData();
            }
    }

这使您不必编写新的相机应用程序,但这段代码也不是很好.最大的问题是

this saves you having to write a new camera app, but this code isn't great either. the big problems are

  1. 你永远不会从有错误的设备.你得到512px 宽的图片被插入到图像内容中提供者.在没有错误,一切都像文档一样,你会得到一张普通的大照片.

  1. you never get full sized images from the devices with the bug. you get pictures that are 512px wide that are inserted into the image content provider. on devices without the bug, everything works as document, you get a big normal picture.

你必须维护这个列表.作为写的,它是可能的设备用一个版本闪烁android(比如 cyanogenmod 的builds) 修复了错误.如果发生这种情况,您的代码将碰撞.解决方法是使用整个设备指纹.

you have to maintain the list. as written, it is possible for devices to be flashed with a version of android (say cyanogenmod's builds) that has the bug fixed. if that happens, your code will crash. the fix is to use the entire device fingerprint.

这篇关于Android ACTION_IMAGE_CAPTURE 意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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