拍摄图像在安卓MediaStore.ACTION_IM​​AGE_CAPTURE意图 [英] capturing images with MediaStore.ACTION_IMAGE_CAPTURE intent in android

查看:128
本文介绍了拍摄图像在安卓MediaStore.ACTION_IM​​AGE_CAPTURE意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我捕捉使用MediaStore.ACTION_IM​​AGE_CAPTURE意图的图像。它工作正常,在大多数的设备。但它不能正常工作按预期一些最新的Andr​​oid设备。

I am capturing images using MediaStore.ACTION_IMAGE_CAPTURE intent. it is working fine in most of the devices. but it is not working correctly in some latest android device as expected.

我的本意是用捕捉图像的相机,并将其发送到服务器而不是存储在图像设备的默认的画廊。

my intention is capture image using camera and send it to the server but not to store that image in default gallery of the device.

*的 * 的:当我拍摄的图像,它返回一些其他的画廊图像onActivityResult方法,而不是拍摄的图像中一些最新的Andr​​oid设备。 我使用低于code捕捉和存储图像。

**: When i capture image, it is returning some other gallery image in onActivityResult method instead of captured image in some latest android devices. I am using below code to capture and store images.

public void launchCamera(View v) {
    Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(camera, CAMERA_PIC_REQUEST );
}

在onActivityResult方法,

In onActivityResult method,

String[] projection = { MediaStore.Images.ImageColumns.SIZE,
                    MediaStore.Images.ImageColumns.DISPLAY_NAME,
                    MediaStore.Images.ImageColumns.DATA, BaseColumns._ID, };
            Cursor c = null;
            Uri u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            try {
                if (u != null) {
                    c = managedQuery(u, projection, null, null, null);
                }
                if ((c != null) && (c.moveToLast())) {
                    Bitmap thumbnail = getBitMapFromLocalPath(c.getString(2), 3);
                    idsImagesgot.add(thumbnail);
                    ContentResolver cr = getContentResolver();
                    cr.delete( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                            BaseColumns._ID + "=" + c.getString(3), null);
                }
            } finally {
                if (c != null) {
                    c.close();
                }
            }

任何一个可以帮助我在这方面。

Can any one help me out in this regards.

在此先感谢。

Sathish所在

推荐答案

照片所采取的ACTION_IM​​AGE_CAPTURE没有在MediaStore所有设备上自动注册。

Photos taken by the ACTION_IMAGE_CAPTURE are not registered in the MediaStore automatically on all devices.

Android官方指南给出了例子: <一href="http://developer.android.com/guide/topics/media/camera.html#intent-receive">http://developer.android.com/guide/topics/media/camera.html#intent-receive 但是,这并不在所有设备上工作的。

The official Android guide gives that example: http://developer.android.com/guide/topics/media/camera.html#intent-receive But that does not work either on all devices.

唯一可靠的方法,我所知道的在于保存路径的画面在一个局部变量。要注意的是,而在后台(而相机应用程序正在运行),您的应用程序可能会被杀死,所以你必须在的onSaveInstanceState保存路径。

The only reliable method I am aware of consists in saving the path to the picture in a local variable. Beware that your app may get killed while in background (while the camera app is running), so you must save the path during onSaveInstanceState.

后评论编辑:

创建那里的照片开始的意图时,将存储的临时文件名。

Create a temporary file name where the photo will be stored when starting the intent.

File tempFile = File.createTempFile("my_app", ".jpg");
fileName = tempFile.getAbsolutePath();
Uri uri = Uri.fromFile(tempFile);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, PICTURE_REQUEST_CODE);

文件名是一个字符串,你的一个活动领域。您必须保存这种方式:

fileName is a String, a field of your activity. You must save it that way:

@Override
public void onSaveInstanceState(Bundle bundle)
{
 super.onSaveInstanceState(bundle);
 bundle.putString("fileName", fileName);
}

和恢复它的onCreate():

and recover it in onCreate():

public void onCreate(Bundle savedInstanceState)
{
 if (savedInstanceState != null)
  fileName = savedInstanceState.getString("fileName");
 // ...
}

现在,在onActivityResult的时候,你知道照片是存储所在的文件名(文件名)。你可以做你希望与任何东西,然后将其删除。

Now, at the time of onActivityResult, you know the name of the file where the photo was stored (fileName). You can do anything you wish with it, and then delete it.

2013年9月19日编辑:看来,一些摄像头的应用程序忽略putExtra URI,和PIC存储在不同的地方。所以,使用文件名的前值您应该检查的目的是否为空或不是。如果你得到一个非空的意图,那么你应该preFER intent.getData()文件名。使用文件名作为一个备份解决方案是必要的,只有当它。

2013-09-19 edit: It seems that some camera apps ignore the putExtra uri, and store the pic in a different place. So, before using the value of fileName you should check whether the intent is null or not. If you get a non-null intent, then you should prefer intent.getData() over fileName. Use fileName as a backup solution only when it is needed.

这篇关于拍摄图像在安卓MediaStore.ACTION_IM​​AGE_CAPTURE意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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