版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lmy0111ly/article/details/50586455
最近做了安卓仿微信上传图片,果断把大神的demo拷了过来,后来发现上传的时候出现了几个问题,那个博客下老是提醒链接过多,评论不了,就搬到这了##
---------首先先贴大神的demo地址:http://blog.csdn.net/jdsjlzx/article/details/44160603#html
获取的图片就是null,尤其是拍照的,我就把那个FileUtil那个存储bitmap的方法给返回了一个路径,然后就好了,下面是照相机返回的路径
//照相机
public void photo() {
Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(openCameraIntent, TAKE_PICTURE);
}
//照相机返回的图片
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
switch (requestCode) {
case TAKE_PICTURE:
if (Bimp.tempSelectBitmap.size() < 9 && resultCode == RESULT_OK) {
new Thread(new Runnable() {
@Override
public void run() {
String fileName = String.valueOf(System.currentTimeMillis() + ".png");
Bitmap bm = (Bitmap) data.getExtras().get("data");
String path = FileUtils.saveBitmap(bm, fileName);
ImageItem takePhoto = new ImageItem();
takePhoto.setBitmap(bm);
takePhoto.setImagePath(path);
Bimp.tempSelectBitmap.add(takePhoto);
}
}).start();
}
break;
}
}
--------Fileutil中return SDPATH + picName;就可以了,办法有点笨,但是不出错
2.在GridAdapter中的update()方法中,要么给loading()中的else后面添加break;要么改成adapter.notifyDataSetChanged();
之后就不会出现oom,并且导致时不时点不了的情况了
//所选图片适配
@SuppressLint("HandlerLeak")
public class GridAdapter extends BaseAdapter {
private LayoutInflater inflater;
private int selectedPosition = -1;
private boolean shape;
public boolean isShape() {
return shape;
}
public void setShape(boolean shape) {
this.shape = shape;
}
public GridAdapter(Context context) {
inflater = LayoutInflater.from(context);
}
public void update() {
// loading();
adapter.notifyDataSetChanged();
}
public int getCount() {
if (Bimp.tempSelectBitmap.size() == 9) {//最多选9张图片
return 9;
}
return (Bimp.tempSelectBitmap.size() + 1);
}
public Object getItem(int arg0) {
return null;
}
public long getItemId(int arg0) {
return 0;
}
public void setSelectedPosition(int position) {
selectedPosition = position;
}
public int getSelectedPosition() {
return selectedPosition;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_upload_gridview,
parent, false);
holder = new ViewHolder();
holder.image = (ImageView) convertView
.findViewById(R.id.item_grida_image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (Bimp.tempSelectBitmap != null) {
if (position == Bimp.tempSelectBitmap.size()) {
holder.image.setImageBitmap(BitmapFactory.decodeResource(
getResources(), R.mipmap.ic_add_img));//加号
if (position == 9) {//只能选9张图片
holder.image.setVisibility(View.GONE);
}
} else {
holder.image.setImageBitmap(Bimp.tempSelectBitmap.get(position).getBitmap());
imagePath = Bimp.tempSelectBitmap.get(position).getImagePath();//这是相册选中的图片路径,大神有说的
map.put(position, imagePath);
System.out.println("list选中上传的集合---" + map.size());
System.out.println("list选中上传的集合Path---" + map.get(position));
}
}
return convertView;
}
public class ViewHolder {
public ImageView image;
}
Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
adapter.notifyDataSetChanged();
break;
}
super.handleMessage(msg);
}
};
//添加break,就可以调用
public void loading() {
new Thread(new Runnable() {
public void run() {
while (true) {
if (Bimp.max == Bimp.tempSelectBitmap.size()) {
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
break;
} else {
Bimp.max += 1;
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
break;//在这添加
}
}
}
}).start();
}
}