实现Android多张图片选择

转载地址:https://blog.csdn.net/mwq384807683/article/details/70314060,亲测比较好用,转载为了以后需要方便找到,也分享

图片选择器, 支持多图选择和图片预览等功能。

开源地址:https://github.com/open-android/ImageSelector

  1. 支持jitpack
  2. 支持选择多张
  3. 支持选择图片数量上限
  4. 支持图片选择顺序
  5. 支持图片预览


使用步骤

1. 在project的build.gradle添加如下代码(如下图)

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
    }
}

2. 在Module的build.gradle添加依赖

  compile 'com.github.open-android:ImageSelector:0.1.0'
  •  

3. 配置如下权限

 
 
 
 

4.复制如下代码到xml布局

 

5.复制如下代码到java类当中

public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_CODE_SELECT_IMG = 1;
    private static final int MAX_SELECT_COUNT = 9;
    private TextView mContentTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        mContentTv = (TextView) findViewById(R.id.content);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_SELECT_IMG) {
            showContent(data);
            return;
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    private void showContent(Intent data) {
        List paths = ImageSelector.getImagePaths(data);
        if (paths.isEmpty()) {
            mContentTv.setText(R.string.image_selector_select_none);
            return;
        }

        mContentTv.setText(paths.toString());
    }

    public void selectImg(View v) {
        ImageSelector.show(this, REQUEST_CODE_SELECT_IMG, MAX_SELECT_COUNT);
    }}

你可能感兴趣的:(android,ImageSelector)