Search code examples
androidandroid-sdcard

Populating listview from sdcard images


I would like to add images from my sdcard to my listview. Currently I can choose picture from my sdcard by the button click on my UI. The implementation to choose picture is this:

sendPicture.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        mybyte=null;

        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);               
    }
});

The onActivityResult of this implementation is:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) {
        if (requestCode == 1) {

        currImageURI = data.getData();
        myvariable=getRealPathFromURI(currImageURI);
            try {
                mybyte=fileToByteArray(myvariable);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

My aim is convert "mybyte", which is a byte[] variable, to an image and put the image on my listview.

Any help is appreciated.


Solution

  • With such an implimentation,transforming byte[] to Bitmap and store Bitmaps in your adapter source list, you will get an OutOfMemmory error. Will be better to store in your ArrayList not the Bitmap but the path to it, you get it from cursor after chosing bitmap from galery, and load the Bitmap in an AsyncTask in the getView method of your CustomAdapter.Google has a good example on how to do a fancy ListView with ImageViews HERE