Custom ListView Adapter containing Images (Bitmap) from Gallery

928 views Asked by At

I'm trying to create a custom adapter that contains images that the user chooses from his gallery by clicking on btn2 button.

I managed to get the bitmap info of the photo from the gallery and set this bitmap in ImageView, I never tried to do a custom list adapter containing bitmap, any help?

MainActivity

public class MainActivity extends ListActivity {

    ImageView image;
    private static final int SELECT_PHOTO = 100;
    Bitmap yourSelectedImage;
    Bitmap[] pics;
    ArrayAdapter<Bitmap> adapter;

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

        Button btn=(Button) findViewById(R.id.button1);
        Button btn2=(Button)findViewById(R.id.button2);
        image= (ImageView)findViewById(R.id.imageView1);
        btn2.setOnClickListener(CPhoto);
        btn.setOnClickListener(Camera);
        pics= new Bitmap[1];
    }

    View.OnClickListener CPhoto=new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, SELECT_PHOTO);
        }
    };

    View.OnClickListener Camera=new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, 1);
        }
    };    

    public void onActivityAfterResult(int resultCode,int requestCode,Intent data) {
        // TODO Auto-generated method stub
        if(data != null && data.getExtras() != null){
            Bitmap bitmap= (Bitmap) data.getExtras().get("data");
            if(bitmap!=null){
                image.setImageBitmap(bitmap);
                pics[0]= bitmap;
                Adapter adp=new Adapter(this,pics);
                setListAdapter(adp);
            }
        }

    }
    public void onActivityForResult(int resultCode,int requestCode,Intent data) {
        // TODO Auto-generated method stub
        if(data != null && data.getExtras() != null){
            Bitmap bitmap= (Bitmap) data.getExtras().get("data");
            if(bitmap!=null){
                image.setImageBitmap(bitmap);

            }
        }

    }

    protected void onActivityResult(int requestCode, int resultCode, 
               Intent imageReturnedIntent) {
            super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

            switch(requestCode) { 
            case SELECT_PHOTO:
                if(resultCode == RESULT_OK){  
                    Uri selectedImage = imageReturnedIntent.getData();
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};

                    Cursor cursor = getContentResolver().query(
                                       selectedImage, filePathColumn, null, null, null);
                    cursor.moveToFirst();

                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String filePath = cursor.getString(columnIndex);
                    cursor.close();

                    yourSelectedImage = BitmapFactory.decodeFile(filePath);
                }
            }
            image.setImageBitmap(yourSelectedImage);
        }               
}

Adapter

public class Adapter extends ArrayAdapter<Bitmap> {
    private final Context context;
    private final Bitmap[] bm;

    public Adapter(Context context,Bitmap[] bm){
        super(context, R.layout.row,bm);

        this.context=context;
        this.bm=bm;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView1= inflater.inflate(R.layout.row, parent, false);

        ImageView imv=(ImageView) rowView1.findViewById(R.id.imageView1);
        imv.setImageBitmap(bm[position]);

        return rowView1;
    }

For some reason I cant put the row.xml layout and the activity_main layout, my row layout contains imageview in a tablerow in a tablelayout

And my activity_main layout contains two buttons , one button activates the camera so the user can use it, the other button opens the gallery, when the user opens the gallery he chooses an image then I call image.setimagebitmap and the image becomes what the user chose, but its not working in the adapter.

Any help?

0

There are 0 answers