Get last image captured using camera on Android

荒凉一梦 提交于 2019-12-04 19:00:00

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;

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