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

效果图:



第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

   

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin"  
  10.     tools:context=".MainActivity">  
  11.   
  12.     <TextView  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:focusable="true"  
  16.         android:focusableInTouchMode="true"  
  17.         android:text="选择模式"  
  18.         android:textColor="@android:color/black"  
  19.         android:textSize="16sp"  
  20.         android:textStyle="bold" />  
  21.   
  22.     <RadioGroup  
  23.         android:id="@+id/choice_mode"  
  24.         android:layout_width="match_parent"  
  25.         android:layout_height="wrap_content"  
  26.         android:checkedButton="@+id/multi"  
  27.         android:orientation="vertical">  
  28.   
  29.         <RadioButton  
  30.             android:id="@+id/single"  
  31.             android:layout_width="wrap_content"  
  32.             android:layout_height="wrap_content"  
  33.             android:text="选择一张" />  
  34.   
  35.         <RadioButton  
  36.             android:id="@+id/multi"  
  37.             android:layout_width="wrap_content"  
  38.             android:layout_height="wrap_content"  
  39.             android:text="选择多张" />  
  40.   
  41.     </RadioGroup>  
  42.   
  43.     <TextView  
  44.         android:layout_width="match_parent"  
  45.         android:layout_height="wrap_content"  
  46.         android:text="最大选择数量"  
  47.         android:textColor="@android:color/black"  
  48.         android:textSize="16sp"  
  49.         android:textStyle="bold" />  
  50.   
  51.     <EditText  
  52.         android:id="@+id/request_num"  
  53.         android:layout_width="match_parent"  
  54.         android:layout_height="wrap_content"  
  55.         android:enabled="false"  
  56.         android:hint="默认是9张"  
  57.         android:inputType="number" />  
  58.   
  59.     <TextView  
  60.         android:layout_width="match_parent"  
  61.         android:layout_height="wrap_content"  
  62.         android:text="上否可以照相"  
  63.         android:textColor="@android:color/black"  
  64.         android:textSize="16sp"  
  65.         android:textStyle="bold" />  
  66.   
  67.     <RadioGroup  
  68.         android:id="@+id/show_camera"  
  69.         android:layout_width="match_parent"  
  70.         android:layout_height="wrap_content"  
  71.         android:checkedButton="@+id/show"  
  72.         android:orientation="vertical">  
  73.   
  74.         <RadioButton  
  75.             android:id="@+id/show"  
  76.             android:layout_width="wrap_content"  
  77.             android:layout_height="wrap_content"  
  78.             android:text="可以" />  
  79.   
  80.         <RadioButton  
  81.             android:id="@+id/no_show"  
  82.             android:layout_width="wrap_content"  
  83.             android:layout_height="wrap_content"  
  84.             android:text="不可以" />  
  85.   
  86.     </RadioGroup>  
  87.   
  88.     <Button  
  89.         android:id="@+id/button"  
  90.         android:layout_width="match_parent"  
  91.         android:layout_height="wrap_content"  
  92.         android:text="图片选择" />  
  93.   
  94.     <!-- 拿到图片的路径-->  
  95.     <ScrollView  
  96.         android:layout_width="match_parent"  
  97.         android:layout_height="match_parent">  
  98.   
  99.         <TextView  
  100.             android:id="@+id/result"  
  101.             android:layout_width="match_parent"  
  102.             android:layout_height="wrap_content" />  
  103.   
  104.     </ScrollView>  
  105.   
  106. </LinearLayout>  
  107. </span>  

第4布
   在MainActivity中写代码:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;">public class MainActivity extends ActionBarActivity {  
  2.     //      展示选中图片的路径  
  3.     private TextView mResultText;  
  4.     //      选择模式,选择一张或者多张  
  5.     private RadioGroup mChoiceMode;  
  6.     //      是否可以照相  
  7.     private RadioGroup mShowCamera;  
  8.     //      最多选择的数量  
  9.     private EditText mRequestNum;  
  10.     //    选择图片的按钮  
  11.     private Button button;  
  12.    //    存放图片路径的list  
  13.     private ArrayList<String> mSelectPath;  
  14.     //启动actviity的请求码  
  15.     private static final int REQUEST_IMAGE = 2;  
  16.   
  17.   
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_main);  
  22.         initView();  
  23.         myOnclick();  
  24.     }  
  25.   
  26.     @Override  
  27.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  28.         super.onActivityResult(requestCode, resultCode, data);  
  29.         if(requestCode == REQUEST_IMAGE){  
  30.             if(resultCode == RESULT_OK){  
  31.                 mSelectPath = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);  
  32.                 StringBuilder sb = new StringBuilder();  
  33.                 for(String p: mSelectPath){  
  34.                     sb.append(p);  
  35.                     sb.append("\n");  
  36.                 }  
  37.                 mResultText.setText(sb.toString());  
  38.             }  
  39.         }  
  40.     }  
  41.     private void myOnclick() {  
  42. //      模式选择  
  43.         mChoiceMode.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {  
  44.             @Override  
  45.             public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {  
  46.                 if(checkedId == R.id.multi){  
  47.                     mRequestNum.setEnabled(true);  
  48.                 }else{  
  49.                     mRequestNum.setEnabled(false);  
  50.                     mRequestNum.setText("");  
  51.                 }  
  52.             }  
  53.         });  
  54.         button.setOnClickListener(new View.OnClickListener() {  
  55.             @Override  
  56.             public void onClick(View v) {  
  57.                 int selectedMode = MultiImageSelectorActivity.MODE_MULTI;  
  58.   
  59.                 if(mChoiceMode.getCheckedRadioButtonId() == R.id.single){  
  60.                     selectedMode = MultiImageSelectorActivity.MODE_SINGLE;  
  61.                 }else{  
  62.                     selectedMode = MultiImageSelectorActivity.MODE_MULTI;  
  63.                 }  
  64.   
  65.                 boolean showCamera = mShowCamera.getCheckedRadioButtonId() == R.id.show;  
  66.   
  67.                 int maxNum = 9;  
  68.                 if(!TextUtils.isEmpty(mRequestNum.getText())){  
  69.                     maxNum = Integer.valueOf(mRequestNum.getText().toString());  
  70.                 }  
  71.   
  72.                 Intent intent = new Intent(MainActivity.this, MultiImageSelectorActivity.class);  
  73.                 // 是否显示拍摄图片  
  74.                 intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, showCamera);  
  75.                 // 最大可选择图片数量  
  76.                 intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, maxNum);  
  77.                 // 选择模式  
  78.                 intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, selectedMode);  
  79.                 // 默认选择  
  80.                 if(mSelectPath != null && mSelectPath.size()>0){  
  81.                     intent.putExtra(MultiImageSelectorActivity.EXTRA_DEFAULT_SELECTED_LIST, mSelectPath);  
  82.                 }  
  83.                 startActivityForResult(intent, REQUEST_IMAGE);  
  84.             }  
  85.         });  
  86.   
  87.     }  
  88.     /** 
  89.      * 初始化控件 
  90.      */  
  91.     private void initView() {  
  92. //      展示选中图片的路径  
  93.         mResultText = (TextView) findViewById(R.id.result);  
  94. //      选择模式,选择一张或者多张  
  95.         mChoiceMode = (RadioGroup) findViewById(R.id.choice_mode);  
  96. //      是否可以照相  
  97.         mShowCamera = (RadioGroup) findViewById(R.id.show_camera);  
  98. //      最多选择的数量  
  99.         mRequestNum = (EditText) findViewById(R.id.request_num);  
  100. //      选择按钮  
  101.         button = (Button)findViewById(R.id.button);  
  102.   
  103.     }  
  104. }</span>  

源码及依赖库的下载:

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

参考文章:

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


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值