当前位置:   article > 正文

【手把手教】Android图片选取显示(PictureSelecter+GridView)_pictureselector.create

pictureselector.create

1.先看最终效果

1.描述

最近在开发中遇到这样的一个场景:即选取照片后利用网格列表显示图片,且图片有个数限制,对于超过限定个数的照片,永远显示最新的九个,类似于微信朋友圈九宫格

2.解决

选取照片的方式有很多种,这里用的依旧是强大的PictureSelecter,对于网格布局采用GridView

demo做了数据持久化,利用了sp存储

3.详细代码

3.1 MainActivity代码
  1. package com.example.newpicturedemo;
  2. import androidx.annotation.Nullable;
  3. import androidx.appcompat.app.AppCompatActivity;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.SharedPreferences;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.GridView;
  11. import android.widget.ImageView;
  12. import com.google.gson.Gson;
  13. import com.google.gson.reflect.TypeToken;
  14. import com.wildma.pictureselector.PictureBean;
  15. import com.wildma.pictureselector.PictureSelector;
  16. import java.lang.reflect.Type;
  17. import java.util.ArrayList;
  18. public class MainActivity extends AppCompatActivity {
  19. private ImageView mimage_head;
  20. private String path;
  21. private ArrayList<Icon> mData = null;
  22. private ArrayList<Icon> mData2 = null;
  23. private GridView grid_photo;
  24. private MainAdapter mAdapter = null;
  25. private int num = 0;
  26. @Override
  27. protected void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.activity_main);
  30. mimage_head = findViewById(R.id.picture2);
  31. grid_photo = (GridView) findViewById(R.id.grid_photo);
  32. mData = new ArrayList<Icon>();
  33. SharedPreferences sp = getSharedPreferences("SP", Context.MODE_PRIVATE);
  34. String value = sp.getString("picture", "");
  35. if (value==""){
  36. return;
  37. }else{
  38. Gson gson = new Gson();
  39. Type type = new TypeToken<ArrayList<Icon>>(){}.getType();
  40. ArrayList<Icon> list = gson.fromJson(value,type);
  41. for (int i=0;i<list.size();i++){
  42. mData.add(list.get(i));
  43. }
  44. mAdapter = new MainAdapter(this, mData);
  45. grid_photo.setAdapter(mAdapter);
  46. }
  47. }
  48. public void cl(View view) {
  49. PictureSelector.create(MainActivity.this, PictureSelector.SELECT_REQUEST_CODE)
  50. .selectPicture();
  51. }
  52. @Override
  53. protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  54. super.onActivityResult(requestCode, resultCode, data);
  55. if (requestCode == PictureSelector.SELECT_REQUEST_CODE) {
  56. if (data != null) {
  57. PictureBean pictureBean = data.getParcelableExtra(PictureSelector.PICTURE_RESULT);
  58. //操作mData数组,使之存储最新的8张图片数据
  59. if (mData.size() > 8 || mData.size() == 8) {
  60. mData.remove(7);
  61. mData.add(0, new Icon(pictureBean.getPath(), "图标1"));
  62. } else {
  63. mData.add(new Icon(pictureBean.getPath(), ""));
  64. }
  65. //利用Gson,将图片数组存入SharedPreferences
  66. Gson gson = new Gson();
  67. String str = gson.toJson(mData);
  68. SharedPreferences sp = getSharedPreferences("SP", Context.MODE_PRIVATE);
  69. sp.edit().putString("picture",str).apply();
  70. //读取SharedPreferences的值,转换成数组
  71. String value = sp.getString("picture", "");
  72. Type type = new TypeToken<ArrayList<Icon>>(){}.getType();
  73. ArrayList<Icon> list = gson.fromJson(value,type);
  74. mAdapter = new MainAdapter(this, list);
  75. grid_photo.setAdapter(mAdapter);
  76. }
  77. }
  78. }
  79. }

解释:主要利用了PictureSelecer进行图片拍照或者相册选取,选取后在onActivityResult拿到图片结果,利用mData数组操作显示最新8张图片这一功能,其后将数组利用Gson存入SP实现数据持久化。

3.2 MainAdapter代码
  1. package com.example.newpicturedemo;
  2. import android.content.Context;
  3. import android.view.View;
  4. import android.view.ViewGroup;
  5. import android.widget.BaseAdapter;
  6. import android.widget.ImageView;
  7. import com.bumptech.glide.Glide;
  8. import java.io.File;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. public class MainAdapter extends BaseAdapter {
  12. private ArrayList<Icon> icons;
  13. private Context context;
  14. public MainAdapter(Context context, ArrayList<Icon> icons) {
  15. super();
  16. this.context = context;
  17. this.icons = icons;
  18. }
  19. @Override
  20. public int getCount() {
  21. return icons.size();
  22. }
  23. @Override
  24. public Object getItem(int i) {
  25. return icons.get(i);
  26. }
  27. @Override
  28. public long getItemId(int i) {
  29. return 0;
  30. }
  31. @Override
  32. public View getView(int i, View view, ViewGroup viewGroup) {
  33. if (view == null) {
  34. view = View.inflate(context, R.layout.item_grid_icon, null);
  35. }
  36. ImageView imageView = view.findViewById(R.id.img_icon);
  37. Glide.with(context).load(new File(icons.get(i).getiId())).into(imageView);
  38. return view;
  39. }
  40. }
3.3 Bean类Icon代码
  1. package com.example.newpicturedemo;
  2. public class Icon {
  3. private String iId;
  4. private String iName;
  5. public Icon() {
  6. }
  7. public Icon(String iId, String iName) {
  8. this.iId = iId;
  9. this.iName = iName;
  10. }
  11. public String getiId() {
  12. return iId;
  13. }
  14. public String getiName() {
  15. return iName;
  16. }
  17. public void setiId(String iId) {
  18. this.iId = iId;
  19. }
  20. public void setiName(String iName) {
  21. this.iName = iName;
  22. }
  23. }
3.4 MainActivity布局代码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context=".MainActivity">
  9. <LinearLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:orientation="vertical">
  13. <GridView
  14. android:id="@+id/grid_photo"
  15. android:layout_width="match_parent"
  16. android:layout_height="match_parent"
  17. android:numColumns="4" />
  18. <ImageView
  19. android:id="@+id/picture2"
  20. android:layout_width="150dp"
  21. android:onClick="cl"
  22. android:layout_gravity="center"
  23. android:layout_height="150dp"
  24. android:src="@mipmap/ic_add_image"/>
  25. <TextView
  26. android:visibility="gone"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:paddingTop="10dp"
  30. android:text="正面"
  31. android:layout_gravity="center"
  32. android:textColor="@color/black"
  33. android:textSize="18sp" />
  34. </LinearLayout>
  35. </LinearLayout>
3.5 引入PictureSelccter依赖
implementation 'com.google.code.gson:gson:2.8.8'

PictureSelecter的坑,记得在AndroidManifest的Application里加入一句:android:requestLegacyExternalStorage="true"

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/175325
推荐阅读
相关标签
  

闽ICP备14008679号