Android中多图片选择器MultiImageSelector库的使用(仿微信)

效果图:

Android中多图片选择器MultiImageSelector库的使用(仿微信)_第1张图片Android中多图片选择器MultiImageSelector库的使用(仿微信)_第2张图片

Android中多图片选择器MultiImageSelector库的使用(仿微信)_第3张图片Android中多图片选择器MultiImageSelector库的使用(仿微信)_第4张图片

第0步 

  把模块 multi-image-selector 作为你的项目依赖添加到工程中.
 (android studio 导入并依赖)
  导入:File----new----Import Module 

  关联:Ctrl+alt+shift+s----Dependencies----+---Module dependency


第1步 
  在你的 AndroidManifest.xml 文件中添加权限 android.permission.READ_EXTERNAL_STORAGE. 在你的     
  AndroidManifest.xml 文件中添加权限 android.permission.WRITE_EXTERNAL_STORAGE. 别忘了同时在       
  AndroidManifest.xml 中声明 MultiImageSelectorActivity 这个Activity.
  <activity
    android:configChanges="orientation|screenSize"

    android:name="me.nereo.multi_image_selector.MultiImageSelectorActivity" />

第3步
   写布局activity_main.xml

   

<span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:text="选择模式"
        android:textColor="@android:color/black"
        android:textSize="16sp"
        android:textStyle="bold" />

    <RadioGroup
        android:id="@+id/choice_mode"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:checkedButton="@+id/multi"
        android:orientation="vertical">

        <RadioButton
            android:id="@+id/single"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="选择一张" />

        <RadioButton
            android:id="@+id/multi"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="选择多张" />

    </RadioGroup>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="最大选择数量"
        android:textColor="@android:color/black"
        android:textSize="16sp"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/request_num"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:enabled="false"
        android:hint="默认是9张"
        android:inputType="number" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="上否可以照相"
        android:textColor="@android:color/black"
        android:textSize="16sp"
        android:textStyle="bold" />

    <RadioGroup
        android:id="@+id/show_camera"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:checkedButton="@+id/show"
        android:orientation="vertical">

        <RadioButton
            android:id="@+id/show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="可以" />

        <RadioButton
            android:id="@+id/no_show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="不可以" />

    </RadioGroup>

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="图片选择" />

    <!-- 拿到图片的路径-->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/result"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </ScrollView>

</LinearLayout>
</span>

第4布
   在MainActivity中写代码:

<span style="font-size:18px;">public class MainActivity extends ActionBarActivity {
    //      展示选中图片的路径
    private TextView mResultText;
    //      选择模式,选择一张或者多张
    private RadioGroup mChoiceMode;
    //      是否可以照相
    private RadioGroup mShowCamera;
    //      最多选择的数量
    private EditText mRequestNum;
    //    选择图片的按钮
    private Button button;
   //    存放图片路径的list
    private ArrayList<String> mSelectPath;
    //启动actviity的请求码
    private static final int REQUEST_IMAGE = 2;


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

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == REQUEST_IMAGE){
            if(resultCode == RESULT_OK){
                mSelectPath = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
                StringBuilder sb = new StringBuilder();
                for(String p: mSelectPath){
                    sb.append(p);
                    sb.append("\n");
                }
                mResultText.setText(sb.toString());
            }
        }
    }
    private void myOnclick() {
//      模式选择
        mChoiceMode.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                if(checkedId == R.id.multi){
                    mRequestNum.setEnabled(true);
                }else{
                    mRequestNum.setEnabled(false);
                    mRequestNum.setText("");
                }
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int selectedMode = MultiImageSelectorActivity.MODE_MULTI;

                if(mChoiceMode.getCheckedRadioButtonId() == R.id.single){
                    selectedMode = MultiImageSelectorActivity.MODE_SINGLE;
                }else{
                    selectedMode = MultiImageSelectorActivity.MODE_MULTI;
                }

                boolean showCamera = mShowCamera.getCheckedRadioButtonId() == R.id.show;

                int maxNum = 9;
                if(!TextUtils.isEmpty(mRequestNum.getText())){
                    maxNum = Integer.valueOf(mRequestNum.getText().toString());
                }

                Intent intent = new Intent(MainActivity.this, MultiImageSelectorActivity.class);
                // 是否显示拍摄图片
                intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, showCamera);
                // 最大可选择图片数量
                intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, maxNum);
                // 选择模式
                intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, selectedMode);
                // 默认选择
                if(mSelectPath != null && mSelectPath.size()>0){
                    intent.putExtra(MultiImageSelectorActivity.EXTRA_DEFAULT_SELECTED_LIST, mSelectPath);
                }
                startActivityForResult(intent, REQUEST_IMAGE);
            }
        });

    }
    /**
     * 初始化控件
     */
    private void initView() {
//      展示选中图片的路径
        mResultText = (TextView) findViewById(R.id.result);
//      选择模式,选择一张或者多张
        mChoiceMode = (RadioGroup) findViewById(R.id.choice_mode);
//      是否可以照相
        mShowCamera = (RadioGroup) findViewById(R.id.show_camera);
//      最多选择的数量
        mRequestNum = (EditText) findViewById(R.id.request_num);
//      选择按钮
        button = (Button)findViewById(R.id.button);

    }
}</span>

源码及依赖库的下载:

http://download.csdn.net/detail/zhaihaohao1/9495930

参考文章:

https://github.com/lovetuzitong/MultiImageSelector/blob/master/README_zh.md


你可能感兴趣的:(Android中多图片选择器MultiImageSelector库的使用(仿微信))