Tuesday, October 23, 2012

List images in MediaStore.Images.Media, with multiple selection.

Last exercise how to "List images in MediaStore.Images.Media". It's modified to have multiple selection on the ListView.

List images in MediaStore.Images.Media, with multiple selection.


package com.example.androidlistimages;

import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.content.CursorLoader;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.app.ListActivity;
import android.database.Cursor;

public class MainActivity extends ListActivity {
 
 //define source of MediaStore.Images.Media, internal or external storage
 Uri sourceUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
 //Uri sourceUri = MediaStore.Images.Media.INTERNAL_CONTENT_URI;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        
        String[] from = {MediaStore.MediaColumns.TITLE};
        int[] to = {android.R.id.text1};

        CursorLoader cursorLoader = new CursorLoader(
          this, 
          sourceUri, 
          null, 
          null, 
          null, 
          MediaStore.Audio.Media.TITLE);
        
        Cursor cursor = cursorLoader.loadInBackground();
        
        ListAdapter adapter = new SimpleCursorAdapter(
          this, 
          android.R.layout.simple_list_item_multiple_choice, 
          cursor, 
          from, 
          to, 
          CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        
        getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        
        setListAdapter(adapter);
    }

}


download filesDownload the files.

Read the post "Start activity to send email with multiple images attached, with build-in MediaStore.Images.Media selector" to know how to retrieve the selected images.

No comments:

Post a Comment