如何在Android中使用带有listView的sd卡拍摄图像

时间:2015-08-11 07:51:14

标签: android listview imageview

我有一个ListView显示来自Sd卡的imageView,用户可以在listview中更改image => onClickItem,但我不能从SD卡中取出图像并将其保存在屏幕上。任何人帮助我

我尝试使用Bitmap,但不能使用endcode图像。

public class ListImage extends ActionBarActivity {
ListView list;
private SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_image);
    list=(ListView) findViewById(R.id.list);
    sharedPreferences = getSharedPreferences("Reg", Context.MODE_PRIVATE);
    int c=sharedPreferences.getInt("numImage",0);
    getData(c);
    list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            startActivity(intent);
            return true;
        }
    });
}
public void getData(int a){
    String[] item=new String[a];
    Integer[] img=new Integer[a];
    for (int i=0;i<a;i++){
        item[i]="Demo";
        img[i]=R.drawable.logodangnhap;
        ImageShow adapter=new ImageShow(this,item,img);
        list.setAdapter(adapter);
    }
}
private class ImageShow extends ArrayAdapter<String>{

    private ListImage  context;
    private String[] item;
    private Integer[]img;
    public ImageShow(ListImage context,String[] item,Integer[] img) {
        super(context, R.layout.mylist,item);
        this.context=context;
        this.item=item;
        this.img=img;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater=context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.mylist,null,true);
        TextView txtTitle=(TextView) rowView.findViewById(R.id.item);
        ImageView imageview=(ImageView) rowView.findViewById(R.id.icon);
        imageview.setImageResource(R.drawable.logodangnhap);
        TextView extratxt=(TextView) rowView.findViewById(R.id.textView100);
        extratxt.setText("Gioi Thiệu: "+item[position]);
        return rowView;
    }
}

1 个答案:

答案 0 :(得分:0)

public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),PICK_IMAGE_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
    Uri uri = data.getData();
    try {
    Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
    Drawable d = new BitmapDrawable(getResources(), bitmap);
    imageview.setImageResource(d);

    } catch (IOException e) {
    e.printStackTrace();
    }
}

}