在Android上使用相机拍摄最后一张照片 [英] Get last image captured using camera on Android

查看:183
本文介绍了在Android上使用相机拍摄最后一张照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码使用相机应用程序捕获用户捕获的最后一个图像,并在我的相机应用程序中将其显示给用户(与其他相机应用程序一样,显示最后一次图像捕获的小预览)一个角落):

I am trying to get the last image captured by the user using a camera app using the following code and display it to the user in my camera app's (like other camera apps do, show a small preview of the last image capture in a corner):

String[] projection = new String[]{
                    MediaStore.Images.ImageColumns._ID,
                    MediaStore.Images.ImageColumns.DATA,
                    MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
                    MediaStore.Images.ImageColumns.DATE_TAKEN,
                    MediaStore.Images.ImageColumns.MIME_TYPE
            };
            final Cursor cursor = getContentResolver()
                    .query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null,
                            null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");

            if (cursor.moveToFirst()) {
                Bitmap bm = BitmapFactory.decodeFile(cursor.getString(1));
                imagePreview.setImageBitmap(bm);
            }

但代码显示我手机中的最后一张图片,如果我拿截图然后它告诉我。我想显示从DCIM文件夹中捕获的最后一张图像,或者保存照片的文件夹,而不是我下载或截图的照片。任何人都可以帮助我吗?

but the code shows me the last image in my phone from anywhere, like if i take a screenshot then it shows me that. I want to display the last image captured from the DCIM folder or whichever folder the photos are kept, not photos I download or screenshot. Can anyone help me out?

推荐答案

没有官方观察员。拍摄媒体图像时,您可能希望使用FileWatcher或观察者。你需要一个接收器来监听摄像头,然后在媒体商店查询最后更新的图像。

there's no official observer for that. You may want to use a FileWatcher or an observer when a media image has been taken. You will need a receiver which listen for the camerashot and then query the mediastore for the last updated image.

// in the receivers class in onChange() 
Cursor cursor = context.getContentResolver().query(uri, null, null, null, "date_added DESC LIMIT 1");
Media media = null;
if (cursor.moveToNext()) {
     int dataColumn = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
     String filePath = cursor.getString(dataColumn);
 }

在cyanogenmod中有一个Intent Filter标志(直到现在才工作)

In the cyanogenmod there's an Intent Filter flag (which is working until now)

 <receiver
        android:name="com.app.CameraEventReceiver"
        android:enabled="true">
        <intent-filter>
            <action android:name="com.android.camera.NEW_PICTURE"/>
            <data android:mimeType="image/*"/>
        </intent-filter>
    </receiver> 

这篇关于在Android上使用相机拍摄最后一张照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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