[Android/Java] Please only import images from specific directories!!

Asked 2 years ago, Updated 2 years ago, 101 views

package org.techtown.hello;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class PhotoUtil {

    String strImage;

    public ArrayList<Listitem> getAllPhotoPathList(Context context) {
        ArrayList<Listitem> photos = new ArrayList<>();

        Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

        String[] projection = {
                MediaStore.Images.Media._ID,
                MediaStore.MediaColumns.DATA,
                MediaStore.Images.Media.DATA,
        };

        Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);

        // Photo ID
        int columnIndexId = cursor.getColumnIndex(MediaStore.Images.Media._ID);

        // Photo path
        int columnIndexData = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);

        //Photo data
        int nCol = cursor.getColumnIndex(MediaStore.Images.Media.DATA); // bitmap

        while (cursor.moveToNext()) {
            strImage = cursor.getString(nCol);

            //if( strImage.startsWith("Environment.getExternalStorageDirectory().getAbsolutePath()"))
            //{
                Listitem Photo = new Listitem(cursor.getString(columnIndexId), cursor.getString(columnIndexData));
                photos.add(Photo);
            //}
        }

        cursor.close();

        // Sort by most recent
        Collections.sort(photos, new DescendingId());

        return photos;
    }

    class DescendingId implements Comparator<Listitem> {
        @Override
        public int compare(Listitem Photo, Listitem t1) {
            return ((Integer)Integer.parseInt(t1.getId())).compareTo((Integer)Integer.parseInt(Photo.getId()));
        }
    }

}

If you code like this, you're going to load all the image files, but if you want to load only the images in a particular folder, how do you change them? I tried running startwith, but it doesn't pop up <

The image is located in Environment.getExternalStorageDirectory()+"/AP/album".

cursor mediastore uri android

2022-09-21 19:03

1 Answers

It's a self-answer.

package org.techtown.hello;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class PhotoUtil {

    String strImage;

    public ArrayList<Listitem> getAllPhotoPathList(Context context) {
        ArrayList<Listitem> photos = new ArrayList<>();

        Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

        String[] projection = {
                MediaStore.Images.Media._ID,
                MediaStore.MediaColumns.DATA,
                MediaStore.Images.Media.DATA,
        };

        Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);

        // Photo ID
        int columnIndexId = cursor.getColumnIndex(MediaStore.Images.Media._ID);

        // Photo path
        int columnIndexData = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);

        //Photo data
        int nCol = cursor.getColumnIndex(MediaStore.Images.Media.DATA); // bitmap

        while (cursor.moveToNext()) {
            strImage = cursor.getString(nCol);

            if( strImage.startsWith(Environment.getExternalStorageDirectory().getAbsolutePath()))
            {
                Listitem Photo = new Listitem(cursor.getString(columnIndexId), cursor.getString(columnIndexData));
                photos.add(Photo);
            }
        }

        cursor.close();

        // Sort by most recent
        Collections.sort(photos, new DescendingId());

        return photos;
    }

    class DescendingId implements Comparator<Listitem> {
        @Override
        public int compare(Listitem Photo, Listitem t1) {
            return ((Integer)Integer.parseInt(t1.getId())).compareTo((Integer)Integer.parseInt(Photo.getId()));
        }
    }

}


2022-09-21 19:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.