Nuke Olaf - Log Store

[Android] 안드로이드 - 기기의 가장 최근 사진을 가져오는 방법 본문

Android

[Android] 안드로이드 - 기기의 가장 최근 사진을 가져오는 방법

NukeOlaf 2020. 1. 14. 01:43

https://stackoverflow.com/questions/41442538/get-the-latest-image-from-external-storage-in-android/41442632

 

Get the latest image from external storage in android

I want to load the latest image from the external storage in Android. Do you have any ideas how to get the name of the latest image? Currently, I load a certain picture from the gallery t...

stackoverflow.com

 

String[] projection = new String[]{
    MediaStore.Images.ImageColumns._ID,
    MediaStore.Images.ImageColumns.DATA,
    MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME, //the album it in
    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);         
    }
} 
private fun setLatestImage() {
        var projection = arrayOf(
            MediaStore.Images.ImageColumns._ID,
            MediaStore.Images.ImageColumns.DATA,
            MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
            MediaStore.Images.ImageColumns.DATE_TAKEN,
            MediaStore.Images.ImageColumns.MIME_TYPE
        )
        val cursor = baseContext.contentResolver
            .query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null,
                MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC")

        if (cursor!!.moveToFirst()) {
            var latestImageUri = cursor.getString(1)
            var imageFile = File(latestImageUri)
            if (imageFile.exists()) {
                var bitmap = BitmapFactory.decodeFile(latestImageUri)
                camera_imgView_album.setImageBitmap(bitmap)
            }
        }
    }
Comments