Android 选择图片、上传图片之ImagePicker

举报
yechaoa 发表于 2022/05/30 22:14:34 2022/05/30
【摘要】 效果图: 后来又出了两篇,也可以看一下 Android 选择图片、上传图片之PictureSelector Android 选择图片、上传图片之Matisse 添加依赖: 选择图片:compile 'com.lzy.widget:imagepicker:0.5.4' github地址:https://github.com/...

效果图:




后来又出了两篇,也可以看一下

Android 选择图片、上传图片之PictureSelector

Android 选择图片、上传图片之Matisse


添加依赖:

选择图片:compile 'com.lzy.widget:imagepicker:0.5.4'

github地址:https://github.com/jeasonlzy/ImagePicker

上传文件:compile 'com.zhy:okhttputils:2.6.2'

github地址:https://github.com/hongyangAndroid/okhttputils

MainActivity.java


  
  1. package com.yechaoa.uploadimage;
  2. import android.content.Intent;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.support.v7.widget.GridLayoutManager;
  6. import android.support.v7.widget.RecyclerView;
  7. import android.view.View;
  8. import android.widget.AdapterView;
  9. import com.lzy.imagepicker.ImagePicker;
  10. import com.lzy.imagepicker.bean.ImageItem;
  11. import com.lzy.imagepicker.ui.ImageGridActivity;
  12. import com.lzy.imagepicker.ui.ImagePreviewDelActivity;
  13. import com.lzy.imagepicker.view.CropImageView;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import okhttp3.Call;
  17. public class MainActivity extends AppCompatActivity implements ImagePickerAdapter.OnRecyclerViewItemClickListener{
  18. public static final int IMAGE_ITEM_ADD = -1;
  19. public static final int REQUEST_CODE_SELECT = 100;
  20. public static final int REQUEST_CODE_PREVIEW = 101;
  21. private ImagePickerAdapter adapter;
  22. private ArrayList<ImageItem> selImageList; //当前选择的所有图片
  23. private int maxImgCount = 8; //允许选择图片最大数
  24. private HttpUtil httpUtil;
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_main);
  29. httpUtil = new HttpUtil();
  30. findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
  31. @Override
  32. public void onClick(View v) {
  33. uploadImage(selImageList);
  34. }
  35. });
  36. //最好放到 Application oncreate执行
  37. initImagePicker();
  38. initWidget();
  39. }
  40. private void initImagePicker() {
  41. ImagePicker imagePicker = ImagePicker.getInstance();
  42. imagePicker.setImageLoader(new GlideImageLoader()); //设置图片加载器
  43. imagePicker.setShowCamera(true); //显示拍照按钮
  44. imagePicker.setCrop(true); //允许裁剪(单选才有效)
  45. imagePicker.setSaveRectangle(true); //是否按矩形区域保存
  46. imagePicker.setSelectLimit(maxImgCount); //选中数量限制
  47. imagePicker.setMultiMode(false); //多选
  48. imagePicker.setStyle(CropImageView.Style.RECTANGLE); //裁剪框的形状
  49. imagePicker.setFocusWidth(800); //裁剪框的宽度。单位像素(圆形自动取宽高最小值)
  50. imagePicker.setFocusHeight(800); //裁剪框的高度。单位像素(圆形自动取宽高最小值)
  51. imagePicker.setOutPutX(1000); //保存文件的宽度。单位像素
  52. imagePicker.setOutPutY(1000); //保存文件的高度。单位像素
  53. }
  54. private void initWidget() {
  55. RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
  56. selImageList = new ArrayList<>();
  57. adapter = new ImagePickerAdapter(this, selImageList, maxImgCount);
  58. adapter.setOnItemClickListener(this);
  59. recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
  60. recyclerView.setHasFixedSize(true);
  61. recyclerView.setAdapter(adapter);
  62. }
  63. private SelectDialog showDialog(SelectDialog.SelectDialogListener listener, List<String> names) {
  64. SelectDialog dialog = new SelectDialog(this, R.style.transparentFrameWindowStyle, listener, names);
  65. if (!this.isFinishing()) {
  66. dialog.show();
  67. }
  68. return dialog;
  69. }
  70. @Override
  71. public void onItemClick(View view, int position) {
  72. switch (position) {
  73. case IMAGE_ITEM_ADD:
  74. List<String> names = new ArrayList<>();
  75. names.add("拍照");
  76. names.add("相册");
  77. showDialog(new SelectDialog.SelectDialogListener() {
  78. @Override
  79. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  80. switch (position) {
  81. case 0: // 直接调起相机
  82. //打开选择,本次允许选择的数量
  83. ImagePicker.getInstance().setSelectLimit(maxImgCount - selImageList.size());
  84. Intent intent = new Intent(MainActivity.this, ImageGridActivity.class);
  85. intent.putExtra(ImageGridActivity.EXTRAS_TAKE_PICKERS,true); // 是否是直接打开相机
  86. startActivityForResult(intent, REQUEST_CODE_SELECT);
  87. break;
  88. case 1:
  89. //打开选择,本次允许选择的数量
  90. ImagePicker.getInstance().setSelectLimit(maxImgCount - selImageList.size());
  91. Intent intent1 = new Intent(MainActivity.this, ImageGridActivity.class);
  92. startActivityForResult(intent1, REQUEST_CODE_SELECT);
  93. break;
  94. default:
  95. break;
  96. }
  97. }
  98. }, names);
  99. break;
  100. default:
  101. //打开预览
  102. Intent intentPreview = new Intent(this, ImagePreviewDelActivity.class);
  103. intentPreview.putExtra(ImagePicker.EXTRA_IMAGE_ITEMS, (ArrayList<ImageItem>) adapter.getImages());
  104. intentPreview.putExtra(ImagePicker.EXTRA_SELECTED_IMAGE_POSITION, position);
  105. intentPreview.putExtra(ImagePicker.EXTRA_FROM_ITEMS,true);
  106. startActivityForResult(intentPreview, REQUEST_CODE_PREVIEW);
  107. break;
  108. }
  109. }
  110. @Override
  111. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  112. super.onActivityResult(requestCode, resultCode, data);
  113. if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
  114. //添加图片返回
  115. if (data != null && requestCode == REQUEST_CODE_SELECT) {
  116. ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
  117. if (images != null){
  118. selImageList.addAll(images);
  119. adapter.setImages(selImageList);
  120. }
  121. }
  122. } else if (resultCode == ImagePicker.RESULT_CODE_BACK) {
  123. //预览图片返回
  124. if (data != null && requestCode == REQUEST_CODE_PREVIEW) {
  125. ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_IMAGE_ITEMS);
  126. if (images != null){
  127. selImageList.clear();
  128. selImageList.addAll(images);
  129. adapter.setImages(selImageList);
  130. }
  131. }
  132. }
  133. }
  134. private String url="http...";
  135. private void uploadImage(ArrayList<ImageItem> pathList) {
  136. httpUtil.postFileRequest(url, null, pathList, new MyStringCallBack() {
  137. @Override
  138. public void onError(Call call, Exception e, int id) {
  139. super.onError(call, e, id);
  140. }
  141. @Override
  142. public void onResponse(String response, int id) {
  143. super.onResponse(response, id);
  144. //返回图片的地址
  145. }
  146. });
  147. }
  148. }


Demo地址:https://github.com/yechaoa/UploadImage



文章来源: blog.csdn.net,作者:yechaoa,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/yechaoa/article/details/70919827

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。