Android ACTION_IMAGE_CAPTURE意图 [英] Android ACTION_IMAGE_CAPTURE Intent

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

问题描述

我们正在尝试使用本机相机应用程序让用户拍摄新照片。它工作很好,如果我们省略了 EXTRA_OUTPUT extra 并返回小的位图图像。但是,如果我们 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 。也就是说,在google的经验builds的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's
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天全站免登陆