ImageSelector是一个图片选择框架,帮助我们轻松实现图片选择功能。
先看效果图
![这里写图片描述](https://img-blog.csdn.net/20160914100750808)
集成步骤
1.build.gradle 加入依赖
compile 'com.androidkun:imageselector:1.0.1'
/**
* PopupWindow
*/
private SelectMothedPopupWindow selectMothedPopupWindow;
/**
* 可选择图片的最大数量
*/
private int selectNum = 3;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectImage();
}
});
}
private void selectImage(){
if(selectMothedPopupWindow == null){
selectMothedPopupWindow = new SelectMothedPopupWindow(this);
}
selectMothedPopupWindow.show(this,findViewById(R.id.main),this);
}
@Override
public void mothedSelected(int mothed) {
if (mothed == 1) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1002);
} else if (mothed == 2) {
startActivityForResult(new Intent(this, SelectImageActivity.class).putExtra("SELECT_NUM", selectNum), 1001);
}
}
4.接收返回结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1001) {
if (resultCode == 1001) {
List<String> images = (List<String>) data.getSerializableExtra("SELECTED_IMAGE");
String image = "";
for (String path : images) {
image += path + "\n";
}
Log.w("TAG",image);
}
}else if(requestCode == 1002) {
if (resultCode == Activity.RESULT_OK) {
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");
}
}
}