Get last image captured using camera on Android

雨燕双飞 提交于 2019-12-06 14:43:48

问题


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);
            }

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?


回答1:


It pretty sure returns all android standard phone's Camera folder, otherwise ask the user to set the Camera folder. also you can provide more conditions to check...

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};

Cursor cursor = getActivity().getContentResolver().query(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        projection,
        null,
        null,
        MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
if(cursor != null){
    cursor.moveToFirst();
    while(cursor.moveToNext()){
        String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));

        if(path.toUpperCase().contains("DCIM/CAMERA") || 
            path.toUpperCase().contains("DCIM/100ANDRO")|| 
            path.toUpperCase().contains("DCIM/100MEDIA")){

        Log.d(TAG,path);
        File file = new File(path);
        path = file.getParent();
        return path;
        }
        continue;
    }
    //cursor.close();
    return null;



回答2:


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);
 }

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> 


来源:https://stackoverflow.com/questions/23458416/get-last-image-captured-using-camera-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!