一分钟实现图片选择功能——ImageSelector

ImageSelector是一个图片选择框架,帮助我们轻松实现图片选择功能。

先看效果图

一分钟实现图片选择功能——ImageSelector_第1张图片

集成步骤

1.build.gradle 加入依赖

compile 'com.androidkun:imageselector:1.0.1'

2.处理按钮点击事件(初始化并弹出PopupWindow)

/** * 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);
    }

//main为父布局
    selectMothedPopupWindow.show(this,findViewById(R.id.main),this);
}

3.监听PopupWindow点击事件

@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) {//相机返回结果(获取Bitmap)
        if (resultCode == Activity.RESULT_OK) {
            Bundle bundle = data.getExtras();
            Bitmap bitmap = (Bitmap) bundle.get("data");
        }
    }
 }

Github地址

你可能感兴趣的:(框架,android,图片选择,相册相机)