欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  技术分享

android批量图片上传_安卓如何选择多张图片_本地多图片选择Demo

程序员文章站 2022-03-01 16:19:20
...

调用系统图库的代码,但是调用之后,点击一张照片图库就会关闭,并将图片数据返回

如果要做一个照片批量上传功能的话,一张一张去选择照片的话,用户绝对会抓狂

先给大家贴一下单选的代码


Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
intent.addCategory(Intent.CATEGORY_OPENABLE); 
intent.setType("image/*"); 
startActivityForResult(intent, CHOOSE_PHOTO); 


调用系统图库,如何一次性选择多张图片,或者直接选择一个图片文件夹啊

经过我查阅资料后发现系统图库是不支持图片多选的(也许是我没有查到吧)

所以就只能挂接选择图片文件夹来实现我们的需求咯

大致实现过程是将本地所有有图片的文件夹先找出来、然后让用户去选择到底是进入那个文件夹、以下是部分代码


public List<FileTraversal> LocalImgFileList(){
    List<FileTraversal> data=new ArrayList<FileTraversal>();
    String filename="";
    List<String> allimglist=listAlldir();
    List<String> retulist=new ArrayList<String>();
    if (allimglist!=null) {
        Set set = new TreeSet();
        String []str;
        for (int i = 0; i < allimglist.size(); i++) {
            retulist.add(getfileinfo(allimglist.get(i)));
        }
        for (int i = 0; i < retulist.size(); i++) {
            set.add(retulist.get(i));
        }
        str= (String[]) set.toArray(new String[0]);
        for (int i = 0; i < str.length; i++) {
            filename=str[i];
            FileTraversal ftl= new FileTraversal();
            ftl.filename=filename;
            data.add(ftl);
        }
        
        for (int i = 0; i < data.size(); i++) {
            for (int j = 0; j < allimglist.size(); j++) {
                if (data.get(i).filename.equals(getfileinfo(allimglist.get(j)))) {
                    data.get(i).filecontent.add(allimglist.get(j));
                }
            }
        }
    }
    return data;
}



然后当用户点击以item的时候、进入另外一个画面、展示该文件夹下面所有图片、以下是部分代码


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.photogrally);
    
    imgGridView=(GridView) findViewById(R.id.gridView1);
    bundle= getIntent().getExtras();
    fileTraversal=bundle.getParcelable("data");
    imgsAdapter=new ImgsAdapter(this, fileTraversal.filecontent,onItemClickClass);
    imgGridView.setAdapter(imgsAdapter);
    select_layout=(LinearLayout) findViewById(R.id.selected_image_layout);
    relativeLayout2=(RelativeLayout) findViewById(R.id.relativeLayout2);
    choise_button=(Button) findViewById(R.id.button3);
    hashImage=new HashMap<Integer, ImageView>();
    filelist=new ArrayList<String>();
//        imgGridView.setOnItemClickListener(this);
    util=new Util(this);
}

class BottomImgIcon implements OnItemClickListener{
    
    int index;
    public BottomImgIcon(int index) {
        this.index=index;
    }
    
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        
    }
}

@SuppressLint("NewApi")
public ImageView iconImage(String filepath,int index,CheckBox checkBox) 
                        throws FileNotFoundException{
    LinearLayout.LayoutParams params=new LayoutParams(relativeLayout2.
    getMeasuredHeight()-10, relativeLayout2.getMeasuredHeight()-10);
    ImageView imageView=new ImageView(this);
    imageView.setLayoutParams(params);
    imageView.setBackgroundResource(R.drawable.imgbg);
    float alpha=100;
    imageView.setAlpha(alpha);
    util.imgExcute(imageView, imgCallBack, filepath);
    imageView.setOnClickListener(new ImgOnclick(filepath,checkBox));
    return imageView;
}

ImgCallBack imgCallBack=new ImgCallBack() {
    @Override
    public void resultImgCall(ImageView imageView, Bitmap bitmap) {
        imageView.setImageBitmap(bitmap);
    }
};

class ImgOnclick implements OnClickListener{
    String filepath;
    CheckBox checkBox;
    public ImgOnclick(String filepath,CheckBox checkBox) {
        this.filepath=filepath;
        this.checkBox=checkBox;
    }
    @Override
    public void onClick(View arg0) {
        checkBox.setChecked(false);
        select_layout.removeView(arg0);
        choise_button.setText("已选择("+select_layout.getChildCount()+")张");
        filelist.remove(filepath);
    }
}



若资源对你有帮助、扫描下方的二维码、关注DD博客微信公众号(ddblogs)吧

最后给贴上Demo的源代码、希望对大家有用、有兴趣的哥们可以下载看看

源代码下载链接: http://dwtedx.com/download.html?bdkey=s/1o6GHvuU 密码: nz74