安卓仿微信上傳圖片問題
阿新 • • 發佈:2018-11-10
最近做了安卓仿微信上傳圖片,果斷把大神的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(); } }