Android图片上传实现预览效果

2019-12-10 19:13:05于海丽

这个是初始化图片数组,适配器(新建、上传失败、上传成功的图片我用的都是一个adapter)

ImageItem是图片的模型,下面有它的属性

//从图库选择的图片model 
public class ImageItem extends File implements Serializable { 
  @Id 
  public String imageId; //图片id 
  public String thumbnailPath;  
  public String imagePath; //图片路径 
  private Bitmap bitmap;  
  public boolean isSelected = false; 
   
  public String getImageId() { 
    return imageId; 
  } 
  public void setImageId(String imageId) { 
    this.imageId = imageId; 
  } 
  public String getThumbnailPath() { 
    return thumbnailPath; 
  } 
  public void setThumbnailPath(String thumbnailPath) { 
    this.thumbnailPath = thumbnailPath; 
  } 
  public String getImagePath() { 
    return imagePath; 
  } 
  public void setImagePath(String imagePath) { 
    this.imagePath = imagePath; 
  } 
  public boolean isSelected() { 
    return isSelected; 
  } 
  public void setSelected(boolean isSelected) { 
    this.isSelected = isSelected; 
  } 
[java] view plain copy print?
/*根据图片路径获取图片的bitmap*/ 
public Bitmap getBitmap() {      
  if(bitmap == null){ 
    try { 
      bitmap = Bimp.revitionImageSize(imagePath); 
    } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
    } 
  } 
  return bitmap; 
} 
public void setBitmap(Bitmap bitmap) { 
  this.bitmap = bitmap; 
} 

 接下来是适配器:

由于涉及到添加图片,adapter中添加一个flag用来显示新建的图片,将选择的图片添加到公有的图片数组中,初始化的时候加载图片数组显示。(大家在看的时候可以忽略掉我的flag)

@SuppressLint("HandlerLeak") 
public class PictureAdapter extends BaseAdapter { 
  private LayoutInflater inflater; 
  private int selectedPosition = -1; 
  private boolean shape; 
  private int flag = 0;//0 默认新建 1上传成功 2上传失败 
  private AppItem_file file; 
  public boolean isShape() { 
    return shape; 
  } 
  private Activity context; 
 
  public void setShape(boolean shape) { 
    this.shape = shape; 
  } 
 
  public PictureAdapter(Activity context,int flag,AppItem_file file) { 
    this.context = context; 
    inflater = LayoutInflater.from(context); 
    this.flag = flag; 
    this.file = file; 
  } 
 
//  public void update() { 
//    loading(); 
//  } 
 
  public int getCount() { 
    if (flag==0){  //新建图片 
      if (Bimp.tempSelectBitmap.size() == 6) { 
        return 6; 
      } 
      return (Bimp.tempSelectBitmap.size() + 1); 
    } 
    else if (flag==1){ //上传成功 
      return file.getFileList().size(); 
    } 
    else { //上传失败 
      return file.getMulFailFilePaths().length; 
    } 
 
  } 
 
 
 
  public Object getItem(int arg0) { 
    if (flag==1){ 
      return file.getFileList().get(arg0); 
    }else { 
      return file.getMulFailFilePaths()[arg0]; 
    } 
  } 
 
  public long getItemId(int arg0) { 
    return arg0; 
  } 
 
  public void setSelectedPosition(int position) { 
    selectedPosition = position; 
  } 
 
  public int getSelectedPosition() { 
    return selectedPosition; 
  } 
 
  public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder = null; 
    if (convertView == null) { 
      //根据图片的数量加载不同布局 
      if (getCount()==1&&flag!=0){ 
        convertView = inflater.inflate(R.layout.item_published_singal_item, 
            parent, false); 
      } 
      else { 
        convertView = inflater.inflate(R.layout.item_published_grida, 
            parent, false); 
      } 
 
      holder = new ViewHolder(); 
      holder.image = (ImageView) convertView 
          .findViewById(R.id.item_grida_image); 
      convertView.setTag(holder); 
    } else { 
      holder = (ViewHolder) convertView.getTag(); 
    } 
 
    /** 
     * 根据初始化adapter传过来的flag值,去不同的地方找图片 
     * flag=0,去Bimp的图片数组中找 
     * flag=1,证明上传成功的,去下载好的getFileList中找 
     * flag=2,为上传失败的,图片保存在FailFile中的List<ImageItem>中 
     * 优化图片显示 
     */ 
    if (flag==0){  //新建图片 
      if (position == Bimp.tempSelectBitmap.size()) { 
        holder.image.setImageBitmap(BitmapFactory.decodeResource( 
            convertView.getResources(), R.drawable.icon_add_pic_unfocused)); 
        if (position == 6) { 
          if (flag==0){ 
            holder.image.setVisibility(View.GONE); 
          } 
        } 
      } else { 
        holder.image.setImageBitmap(Bimp.tempSelectBitmap.get(position).getBitmap()); 
      } 
    } 
    else if (flag==1){ //上传成功 
//      List<Integer> ids = new ArrayList<Integer>(); 
//      for (int i=0;i<file.getFileList().size();i++){ 
//        ids.add(file.getFileList().get(i).getS_id()); 
//      } 
      int id=file.getFileList().get(position).getS_id(); 
      try { 
//        File file= NeedApplication.db.findById(File.class,id); 
        String fileBigImgUri =NeedApplication.db.findById(File.class,id).getFileUriBig(); 
        if (fileBigImgUri!=null&&!"null".equals(fileBigImgUri)) 
        ImageLoader.getInstance().displayImage((fileBigImgUri).trim(),holder.image); 
      } catch (DbException e) { 
        e.printStackTrace(); 
      } 
    } 
    else { //上传失败 
        String url = "file://"+file.getMulFailFilePaths()[position].trim(); 
        ImageLoader.getInstance().displayImage(url, holder.image); 
    } 
 
 
    return convertView; 
  } 
 
  public class ViewHolder { 
    public ImageView image; 
  } 
 
}