Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
2.3 kB
1
Indexable
Never
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

public class YourActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listAllMediaImages();
    }

    private void listAllMediaImages() {
        ContentResolver contentResolver = getContentResolver();
        Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

        // Define the columns you want to retrieve
        String[] projection = {
                MediaStore.Images.Media._ID,
                MediaStore.Images.Media.DATA,
                MediaStore.Images.Media.DISPLAY_NAME,
                MediaStore.Images.Media.DATE_MODIFIED
        };

        // Optionally, you can add a selection (WHERE) clause
        String selection = null;
        String[] selectionArgs = null;

        // Sorting order
        String sortOrder = MediaStore.Images.Media.DATE_MODIFIED + " DESC";

        // Query the external media store for images
        try (Cursor cursor = contentResolver.query(uri, projection, selection, selectionArgs, sortOrder)) {
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    // Retrieve image details
                    long imageId = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID));
                    String imagePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
                    String displayName = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME));
                    long dateModified = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATE_MODIFIED));

                    // You can use these details as needed
                    Log.d("ImageDetails", "ID: " + imageId + ", Path: " + imagePath + ", Name: " + displayName + ", Modified: " + dateModified);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Leave a Comment