包含图库中图像(位图)的自定义ListView适配器

时间:2015-06-17 14:33:11

标签: android image listview bitmap listadapter

我正在尝试创建一个自定义适配器,其中包含用户通过单击btn2按钮从其库中选择的图像。

我设法从图库中获取照片的位图信息并在ImageView中设置此位图,我从未尝试过包含位图的自定义列表适配器,有什么帮助吗?

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);
        }               
}

适配器

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;
    }

由于某种原因,我无法放置row.xml布局和activity_main布局,我的行布局包含tableout中tablerow的imageview

我的activity_main布局包含两个按钮,一个按钮激活相机以便用户可以使用它,另一个按钮打开图库,当用户打开图库时他选择一个图像然后我调用image.setimagebitmap并且图像变为用户选择了什么,但它不适用于适配器。

任何帮助?

0 个答案:

没有答案