get the last picture taken by user
Asked Answered
L

3

22

hey I want to get the last picture captured by user through any camera application. I have no idea how to do that

can any one help me?

further I want to send that image as an attachment to an email or MMS..

thanks

Lurlene answered 1/12, 2011 at 6:55 Comment(1)
I know this isn't quite what you're asking, but maybe it is more what you mean? You can start the camera activity and get the picture the user takes as a result. See here #2315458Homespun
L
60
// Find the last picture
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 = getContext().getContentResolver()
        .query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, 
               null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");

// Put it in the image view
if (cursor.moveToFirst()) {
    final ImageView imageView = (ImageView) findViewById(R.id.pictureView);
    String imageLocation = cursor.getString(1);
    File imageFile = new File(imageLocation);
    if (imageFile.exists()) {   // TODO: is there a better way to do this?
        Bitmap bm = BitmapFactory.decodeFile(imageLocation);
        imageView.setImageBitmap(bm);         
    }
} 

I'm still working on the MMS sending part.

Leeuwarden answered 19/12, 2011 at 4:37 Comment(8)
What is value for DATE_TAKEN?Stillas
@Stillas it's MediaStore.Images.ImageColumns.DATE_TAKENEuchologion
Note that this has now become outdated: managedQuery has been deprecated since Honeycomb (Android 3.0).Commutator
@lunakid It's better to update an answer than make a note that the answer is deprecated. :)Violoncellist
@ShaneCreighton-Young, that's correct. It's also better to leave a quick warning than doing nothing, I think, as sometimes that's just all one can afford. Thanks for your effort to do the "better than better" thing, anyhow. +1 for that. Cheers!Commutator
I don't think this will bring last CAPTURED image. This will bring last image as of date taken. Maybe whatsapp image or instagram image.Transcaucasia
Might also try sorting by MediaStore.Images.ImageColumns._ID + " DESC"Catherine
@Dr.aNdRO This is helpfull if the person is trying to access the pic just after clicking it.Karilynn
B
3

Inspired by https://mcmap.net/q/587074/-how-to-get-last-image-captured-with-camera-in-android-duplicate

So the main concern in that answer was not all the devices are using "DCIM" as the camera folder. Then I found out that if a file is located inside a app-specified folder, it will be indexed by ContentResolver but the other app doesn't have access to it, which means canRead=false. So here I come up with another solution:

    while (cursor.moveToNext()) {
        String imagePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA));
        File imageFile = new File(imagePath);
        if (imageFile.canRead() && imageFile.exists()) {
           // we have found the latest picture in the public folder, do whatever you want
            break;
        }
    }
Berna answered 18/3, 2016 at 16:58 Comment(0)
B
2

Here it is in Kotlin. I have used Glide library to load the last image into an imageview

private fun getLastImageFromGallery(){
        val uriExternal: Uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI

        val projection = arrayOf(
            MediaStore.Images.ImageColumns._ID,
            MediaStore.Images.Media._ID,
            MediaStore.Images.ImageColumns.DATE_ADDED,
            MediaStore.Images.ImageColumns.MIME_TYPE
        )
        val cursor: Cursor = applicationContext.contentResolver.query(uriExternal, projection, null,
            null, MediaStore.Images.ImageColumns.DATE_ADDED + " DESC"
        )!!

        Log.i("Cursor Last", cursor.moveToLast().toString())
        if (cursor.moveToFirst()) {
            val columnIndexID = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID)
            val imageId: Long = cursor.getLong(columnIndexID)
            val imageURI = Uri.withAppendedPath(uriExternal, "" + imageId)

            Glide.with(applicationContext)
                .load(imageURI)
                .transform(CenterCrop(), RoundedCorners(12))
                .into(imageView)

        }

        cursor.close()
} 
Botsford answered 24/12, 2020 at 8:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.