Questions about the android image folder!

Asked 2 years ago, Updated 2 years ago, 136 views

Hello, I'm just asking you this question because it hasn't been a few days since I was implementing the image folder.

You have successfully imported the image through Media.store. So I even took a folder of each picture through Bucket Dispalyname The first problem is that I have to bring a folder for every picture, so it's an array, so I have 1,000 arrays, and I can't bring them to the index because there's no pattern And I succeeded in using the file class to scatter the folders on the sd card as a list view, where I tried to query with something called String.endwith, but it didn't work I'm looking for it on Google, but it doesn't come out well, so I'm asking for help from masters like this. Help me.

String[] project = new String[]{
                MediaStore.Images.Media._ID,
                MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
                MediaStore.Images.Media.DATE_TAKEN
        };


        Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

Cursor cur = managedQuery(images,
                projection, // Which columns to return
                null,       // Which rows to return (all rows)
                null,       // Selection arguments (none)
                null        // Ordering
        );

        Log.i("ListingImages", " query count=" + cur.getCount());

        if (cur.moveToFirst()) {

            int bucketColumn = cur.getColumnIndex(
                    MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
            Log.d(TAG, "bucketColumn" + cur.getString(bucketColumn));

            int dateColumn = cur.getColumnIndex(
                    MediaStore.Images.Media.DATE_TAKEN);

            int id = cur.getColumnIndex(
                    MediaStore.Images.Media._ID);


            do {
                // // Get the field values
                bucket = cur.getString(bucketColumn);
                Log.d(TAG, "bucketColumn" + bucket);
                date = cur.getString(dateColumn);
                String id_str = cur.getString(id);
                temp.add(bucket.toString());
//                //                Log.i("folder", "temp : " + temp);
                // // Do something with the values.
//                //                Log.i("ListingImages", " bucket=" + bucket
//                        //                        + "  date_taken=" + date);
            } } while (cur.moveToNext());

android java image gallery

2022-09-21 19:41

1 Answers

I made a simple example with the source above. I haven't tested it a lot to see if it works well, but I've checked how much it works.

private HashSet<String> mBucketDispalyList;
private ArrayList<String> mFolderArrayList;

private ListView mListView;
private ListAdapter mListAdapter;
private Context mContext;

private static final String[] PROJECTION = new String[] { 
        MediaStore.Images.Media._ID,
        MediaStore.Images.Media.BUCKET_DISPLAY_NAME, 
        MediaStore.Images.Media.DATA,
        MediaStore.Images.Media.TITLE};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mContext = this;
    mListView = (ListView) findViewById(R.id.view_list);

    mBucketDispalyList = new HashSet<String>();

}

@Override
protected void onResume() {
    // // TODO Auto-generated method stub
    super.onResume();
    getBucketDisplayList();
    if (DEBUG) {
        Iterator<String> iterator = mBucketDispalyList.iterator();

        while (iterator.hasNext()) {
            Log.e(TAG, "hashset : " + iterator.next());
        }
    }
    mFolderArrayList = new ArrayList<String>(mBucketDispalyList);
    mListAdapter = new ListAdapter();
    mListAdapter.setData(mFolderArrayList);
    mListView.setAdapter(mListAdapter);
    mListView.setOnItemClickListener(this);
}

public void getBucketDisplayList() {
    String bucket, dataPath;
    Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, PROJECTION, null, null, null);

    if (cursor != null && cursor.moveToFirst()) {

        int bucketColumn = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
        int dataPathColumn = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
        do {
            bucket = cursor.getString(bucketColumn);
            dataPath = cursor.getString(dataPathColumn);
            Log.d(TAG, "bucketColumn : " + bucket);
            mBucketDispalyList.add(bucket.toString());
            Log.e(TAG, "bucket = " + bucket + " , dataPath = " + dataPath);
        } } while (cursor.moveToNext());

        cursor.close();
    }
}

public ArrayList<String> getPictureList(String bucket) {
    String title = null;
    ArrayList<String> pictureList = new ArrayList<String>();
    String selection = MediaStore.Images.Media.BUCKET_DISPLAY_NAME  + " == " + "\""+bucket+"\"";
    Cursor cursor = getContentResolver().query(Images.Media.EXTERNAL_CONTENT_URI, PROJECTION, selection, null, null);

    if (cursor != null && cursor.moveToFirst()) {

        int titleColumn = cursor.getColumnIndex(MediaStore.Images.Media.TITLE);
        do {
            title = cursor.getString(titleColumn);
            Log.d(TAG, "title : " + title);
            pictureList.add(title.toString());
        } } while (cursor.moveToNext());

        cursor.close();
    }

    return pictureList;
}


2022-09-21 19:41

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.