Android开发选取相册多张图片

源代码地址:https://github.com/luoye123/Box

根据昨天写的弹窗呢,完成了显示手机图片并选择的功能。
闲话不多说了,上代码吧,因为代码有点多,我这着重于说一部分!

上一篇弹窗博客地址:http://blog.csdn.net/qq_30299305/article/details/72810252

1、Android6.0以上手机记得动态获取权限哦

private void requestPermission() {
    if (Build.VERSION.SDK_INT >= 23 && !isPermissionRequested) {

        isPermissionRequested = true;

        ArrayList permissions = new ArrayList<>();
        if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
        }

        if (permissions.size() == 0) {
            return;
        } else {
            requestPermissions(permissions.toArray(new String[permissions.size()]), 0);
        }
    }
}

2、点击 从相册选取 跳转的图片显示页面:ImageActivity.java

public  class ImageActivity extends AppCompatActivity {

//显示手机里的所有图片的列表控件
private GridView gridView;
//当手机里没有图片时,提示用户没有图片的控件
private TextView tv;
//gridView的adapter
private ImageGridViewAdapter gridImageAdapter;
//完成按钮
private Button okButton;
// 返回按钮
private Button back;
// 取消按钮
private Button cancel;
private Intent intent;
// 预览按钮
private Button preview;
private Context mContext;
private ArrayList dataList;
private ImageHelper helper;
public static List contentList;
public static Bitmap bitmap;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.plugin_camera_album);
    mContext = this;
    //注册一个广播,这个广播主要是用于在GalleryActivity进行预览时,防止当所有图片都删除完后,再回到该页面时被取消选中的图片仍处于选中状态
    IntentFilter filter = new IntentFilter("data.broadcast.action");
    registerReceiver(broadcastReceiver, filter);
    bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.plugin_camera_no_pictures);
    init();
    initListener();
    //这个函数主要用来控制预览和完成按钮的状态
    isShowOkBt();
}

BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        //mContext.unregisterReceiver(this);
        // TODO Auto-generated method stub
        gridImageAdapter.notifyDataSetChanged();
    }
};

// 预览按钮的监听
private class PreviewListener implements View.OnClickListener {
    public void onClick(View v) {
        if (Bimp.tempSelectBitmap.size() > 0) {
            intent.putExtra("position", "1");
            intent.setClass(ImageActivity.this, GalleryActivity.class);
            startActivity(intent);
        }
    }

}

// 完成按钮的监听
private class AlbumSendListener implements View.OnClickListener {
    public void onClick(View v) {
        finish();
    }

}

// 返回按钮监听
private class BackListener implements View.OnClickListener {
    public void onClick(View v) {
        return;
    }
}

// 取消按钮的监听
private class CancelListener implements View.OnClickListener {
    public void onClick(View v) {
        Bimp.tempSelectBitmap.clear();
        finish();
    }
}



// 初始化,给一些对象赋值
private void init() {
    helper = ImageHelper.getHelper();
    helper.init(getApplicationContext());

    contentList = helper.getImagesBucketList(false);
    dataList = new ArrayList();
    for(int i = 0; i= 9) {
                        toggleButton.setChecked(false);
                        chooseBt.setVisibility(View.GONE);
                        if (!removeOneData(dataList.get(position))) {
                            Toast.makeText(ImageActivity.this, "超出可选图片张数", Toast.LENGTH_SHORT).show();
                        }
                        return;
                    }
                    if (isChecked) {
                        chooseBt.setVisibility(View.VISIBLE);
                        Bimp.tempSelectBitmap.add(dataList.get(position));
                        okButton.setText("完成"+"(" + Bimp.tempSelectBitmap.size()
                                + "/"+ 9+")");
                    } else {
                        Bimp.tempSelectBitmap.remove(dataList.get(position));
                        chooseBt.setVisibility(View.GONE);
                        okButton.setText("完成"+"(" + Bimp.tempSelectBitmap.size() + "/"+ 9+")");
                    }
                    isShowOkBt();
                }
            });

    okButton.setOnClickListener(new AlbumSendListener());

}

private boolean removeOneData(ImageBean ImageBean) {
    if (Bimp.tempSelectBitmap.contains(ImageBean)) {
        Bimp.tempSelectBitmap.remove(ImageBean);
        okButton.setText("完成"+"(" +Bimp.tempSelectBitmap.size() + "/"+ 9+")");
        return true;
    }
    return false;
}

public void isShowOkBt() {
    if (Bimp.tempSelectBitmap.size() > 0) {
        okButton.setText("完成"+"(" + Bimp.tempSelectBitmap.size() + "/"+ 9+")");
        preview.setPressed(true);
        okButton.setPressed(true);
        preview.setClickable(true);
        okButton.setClickable(true);
        okButton.setTextColor(Color.WHITE);
        preview.setTextColor(Color.WHITE);
    } else {
        okButton.setText("完成"+"(" + Bimp.tempSelectBitmap.size() + "/"+ 9+")");
        preview.setPressed(false);
        preview.setClickable(false);
        okButton.setPressed(false);
        okButton.setClickable(false);
        okButton.setTextColor(Color.parseColor("#E1E0DE"));
        preview.setTextColor(Color.parseColor("#E1E0DE"));
    }
}

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        finish();
    }
    return false;

}
@Override
protected void onRestart() {
    isShowOkBt();
    super.onRestart();
}

}

3、以及对应的activity_image.xml





    

东西太多,就不一 一讲述了,有兴趣的话,可以通过上面源码地址下载来进行研究。

你可能感兴趣的:(Android学习)