From 49ce9d2a9bd32c1ffe5339a64ba1780b1c5dd8be Mon Sep 17 00:00:00 2001 From: fyf Date: Wed, 5 Jul 2023 16:28:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BD=91=E7=BB=9C=E5=9B=BE?= =?UTF-8?q?=E7=89=872?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 3 + .../bonait/bnframework/MainApplication.java | 2 + .../bnframework/business/ConfigData.java | 2 + .../common/image/utils/LocalCacheUtils.java | 14 +- .../common/image/utils/MD5Encoder.java | 36 +++-- .../home/adapter/image_sp_adapter.java | 131 ++++++++++++++++++ .../fragment/from/ImageChooseActivity.java | 100 +++++++++++++ .../modules/home/fragment/mode/image_sp.java | 8 ++ .../main/res/layout/activity_image_choose.xml | 29 ++++ .../res/layout/item/layout/item_image_sp.xml | 19 +++ 10 files changed, 329 insertions(+), 15 deletions(-) create mode 100644 app/src/main/java/com/bonait/bnframework/modules/home/adapter/image_sp_adapter.java create mode 100644 app/src/main/java/com/bonait/bnframework/modules/home/fragment/from/ImageChooseActivity.java create mode 100644 app/src/main/java/com/bonait/bnframework/modules/home/fragment/mode/image_sp.java create mode 100644 app/src/main/res/layout/activity_image_choose.xml create mode 100644 app/src/main/res/layout/item/layout/item_image_sp.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 7dd48c50..85f0d9e1 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -28,6 +28,9 @@ android:theme="@style/AppTheme" tools:ignore="GoogleAppIndexingWarning" tools:node="merge"> + diff --git a/app/src/main/java/com/bonait/bnframework/MainApplication.java b/app/src/main/java/com/bonait/bnframework/MainApplication.java index 71cd2d54..edd0507b 100644 --- a/app/src/main/java/com/bonait/bnframework/MainApplication.java +++ b/app/src/main/java/com/bonait/bnframework/MainApplication.java @@ -1,5 +1,6 @@ package com.bonait.bnframework; + import android.annotation.SuppressLint; import android.app.Activity; import android.app.Application; @@ -110,6 +111,7 @@ public class MainApplication extends Application { //初始化DB数据 InitDBdata(); DataBus.getInstance().GetLc();//获取料仓数据 + } //========================================================================// diff --git a/app/src/main/java/com/bonait/bnframework/business/ConfigData.java b/app/src/main/java/com/bonait/bnframework/business/ConfigData.java index 212854c6..6ff5e983 100644 --- a/app/src/main/java/com/bonait/bnframework/business/ConfigData.java +++ b/app/src/main/java/com/bonait/bnframework/business/ConfigData.java @@ -204,11 +204,13 @@ public class ConfigData { BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize=2;//宽高压缩为原来的1/2 options.inPreferredConfig=Bitmap.Config.ARGB_4444; + Bitmap bitmap0= BitmapFactory.decodeResource(context.getResources(), R.mipmap.loading3,options); Bitmap bitmap1= BitmapFactory.decodeResource(context.getResources(), R.mipmap.image1,options); Bitmap bitmap2= BitmapFactory.decodeResource(context.getResources(), R.mipmap.image2,options); Bitmap bitmap3= BitmapFactory.decodeResource(context.getResources(), R.mipmap.image3,options); Bitmap bitmap4= BitmapFactory.decodeResource(context.getResources(), R.mipmap.image4,options); LocalCacheUtils localCacheUtils=new LocalCacheUtils(); + localCacheUtils.setBitmapToLocal("image0.png",bitmap0); localCacheUtils.setBitmapToLocal("image1.png",bitmap1); localCacheUtils.setBitmapToLocal("image2.png",bitmap2); localCacheUtils.setBitmapToLocal("image3.png",bitmap3); diff --git a/app/src/main/java/com/bonait/bnframework/common/image/utils/LocalCacheUtils.java b/app/src/main/java/com/bonait/bnframework/common/image/utils/LocalCacheUtils.java index 87caefd7..88e50181 100644 --- a/app/src/main/java/com/bonait/bnframework/common/image/utils/LocalCacheUtils.java +++ b/app/src/main/java/com/bonait/bnframework/common/image/utils/LocalCacheUtils.java @@ -5,6 +5,7 @@ import android.graphics.BitmapFactory; import android.os.Environment; import com.bonait.bnframework.common.constant.ConfigName; +import com.bonait.bnframework.common.helper.AES; import java.io.File; import java.io.FileInputStream; @@ -24,11 +25,13 @@ public class LocalCacheUtils { public Bitmap getBitmapFromLocal(String url){ String fileName = null;//把图片的url当做文件名,并进行MD5加密 try { - fileName = MD5Encoder.encode(url); + fileName = MD5Encoder.encode(url);//AES.Encrypt(url);//MD5Encoder.encode(url); File file=new File(CACHE_PATH,fileName); - - Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(file)); - + Bitmap bitmap=null; + if(file.exists())// + { + bitmap = BitmapFactory.decodeStream(new FileInputStream(file)); + } return bitmap; } catch (Exception e) { e.printStackTrace(); @@ -44,7 +47,8 @@ public class LocalCacheUtils { */ public void setBitmapToLocal(String url,Bitmap bitmap){ try { - String fileName = MD5Encoder.encode(url);//把图片的url当做文件名,并进行MD5加密 + + String fileName = MD5Encoder.encode(url);;//AES.Encrypt(url);//;MD5Encoder.encode(url);//把图片的url当做文件名,并进行MD5加密 File file=new File(CACHE_PATH,fileName); //通过得到文件的父文件,判断父文件是否存在 diff --git a/app/src/main/java/com/bonait/bnframework/common/image/utils/MD5Encoder.java b/app/src/main/java/com/bonait/bnframework/common/image/utils/MD5Encoder.java index b4bb2897..e3b29550 100644 --- a/app/src/main/java/com/bonait/bnframework/common/image/utils/MD5Encoder.java +++ b/app/src/main/java/com/bonait/bnframework/common/image/utils/MD5Encoder.java @@ -3,21 +3,37 @@ package com.bonait.bnframework.common.image.utils; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +/** + * Md5加密 + * 已经加密的就不在加密 + */ public class MD5Encoder { + /** + * 加密 + * @param pwd + * @return + */ public static String encode(String pwd) { try { - MessageDigest digest = MessageDigest.getInstance("MD5"); - byte[] bytes = digest.digest(pwd.getBytes()); - StringBuffer sb = new StringBuffer(); - for(int i = 0;i { + + private final LayoutInflater mLayoutInflater; + + private Context context; + + List goods = new ArrayList<>(); + + /** + * 点击事件 + */ + public MyClickListener mListener=null; + + public image_sp_adapter(Context context,List data) { + this.context = context; + mLayoutInflater = LayoutInflater.from(context); + goods=data; + } + + @NonNull + @Override + public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View inflate = mLayoutInflater.inflate(R.layout.item_image_sp, parent, false); + return new MyLCViewHolder1(inflate); + + } + + @Override + public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { + try { + if (holder instanceof MyLCViewHolder1) { + MyLCViewHolder1 myViewHolder = (MyLCViewHolder1) holder; + new MyBitmapUtils().disPlay(myViewHolder.img,goods.get(position).url); + myViewHolder.img.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + //按钮点击 + String title = "选择菜品操作提示!"; + String message = "请问客官确定要选择该图片吗?"; + AlertDialogUtils.showDialog(context, title, message, new QMUIDialogAction.ActionListener() { + @Override + public void onClick(QMUIDialog dialog, int index) { + if(mListener!=null) + { + mListener.clickListener(view,goods.get(position)); + } + dialog.dismiss(); + } + }); + } + }); + + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + private Activity findActivity(@NonNull Context context) { + if (context instanceof Activity) { + return (Activity) context; + } else if (context instanceof ContextWrapper) { + return findActivity(((ContextWrapper) context).getBaseContext()); + } else { + return null; + } + } + + /** + * 刷新 + * + * @param + */ + public void refresh() { + Activity activity = findActivity(context); + if (activity != null) { + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + try { + notifyDataSetChanged(); + } catch (Exception e) { + + } + } + }); + } + } + + @Override + public int getItemCount() { + return goods.size(); + } + + public static class MyLCViewHolder1 extends RecyclerView.ViewHolder { + ImageView img;//图片按钮 + + public MyLCViewHolder1(View view) { + super(view); + img=view.findViewById(R.id.img); + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/bonait/bnframework/modules/home/fragment/from/ImageChooseActivity.java b/app/src/main/java/com/bonait/bnframework/modules/home/fragment/from/ImageChooseActivity.java new file mode 100644 index 00000000..e374e1be --- /dev/null +++ b/app/src/main/java/com/bonait/bnframework/modules/home/fragment/from/ImageChooseActivity.java @@ -0,0 +1,100 @@ +package com.bonait.bnframework.modules.home.fragment.from; + +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.SearchView; +import androidx.recyclerview.widget.RecyclerView; + +import android.os.Bundle; +import android.os.Environment; +import android.view.View; + +import com.bonait.bnframework.R; +import com.bonait.bnframework.common.base.BaseActivity; +import com.bonait.bnframework.common.constant.DataBus; +import com.bonait.bnframework.common.db.mode.BPA_GOODS; +import com.bonait.bnframework.common.helper.AES; +import com.bonait.bnframework.common.helper.I.MyClickListener; +import com.bonait.bnframework.common.utils.ToastUtils; +import com.bonait.bnframework.common.view.MyLayoutManager; +import com.bonait.bnframework.modules.home.adapter.image_sp_adapter; +import com.bonait.bnframework.modules.home.adapter.sp_adapter; +import com.bonait.bnframework.modules.home.fragment.mode.image_sp; +import com.qmuiteam.qmui.widget.QMUITopBar; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import butterknife.BindView; +import butterknife.ButterKnife; + +public class ImageChooseActivity extends BaseActivity { + + @BindView(R.id.topbar) + QMUITopBar mTopBar;//顶部标题 + @BindView(R.id.recycler_view) + RecyclerView recyclerView;//image列表 + private static final String CACHE_PATH= Environment.getExternalStorageDirectory().getAbsolutePath()+"/hblxiaochaodb/WebImage"; + List data =new ArrayList<>(); + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_image_choose); + ButterKnife.bind(this); + initTopBar(); + initData(); + } + + private void initTopBar() { + mTopBar.setTitle("选择图片"); + mTopBar.addLeftImageButton(R.mipmap.fanhui,R.id.topbar).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + finish(); + } + }); + } + + private void initData() { + try{ + File path = new File(CACHE_PATH);// 获得路径 + File[] files = path.listFiles();// 读取文件 + data.clear(); + for (int i = 0; i < files.length; i++) { + String fileName = files[i].getName(); + image_sp s=new image_sp(); + s.url=fileName; + data.add(s); + } + MyLayoutManager layout = new MyLayoutManager(); + layout.setAutoMeasureEnabled(true); + recyclerView.setLayoutManager(layout); + image_sp_adapter spadapter = new image_sp_adapter(this,data); + recyclerView.setAdapter(spadapter); + + spadapter.mListener=new MyClickListener() { + @Override + public void clickListener(View v, Object data) { + image_sp sp=(image_sp)data; + ToastUtils.info("选择图片:"+sp.url); + finish(); + } + @Override + public void clickListenerNew(View v, int k, Object data) { + } + }; + }catch(Exception e){ + ToastUtils.info("异常信息:"+e.getMessage()); + } + } + + @Override + public void onDestroy() { + super.onDestroy(); + } + + @Override + protected boolean canDragBack() { + return false; + } +} \ No newline at end of file diff --git a/app/src/main/java/com/bonait/bnframework/modules/home/fragment/mode/image_sp.java b/app/src/main/java/com/bonait/bnframework/modules/home/fragment/mode/image_sp.java new file mode 100644 index 00000000..4ff60c58 --- /dev/null +++ b/app/src/main/java/com/bonait/bnframework/modules/home/fragment/mode/image_sp.java @@ -0,0 +1,8 @@ +package com.bonait.bnframework.modules.home.fragment.mode; + +/** + * 加载图片 + */ +public class image_sp { + public String url; +} diff --git a/app/src/main/res/layout/activity_image_choose.xml b/app/src/main/res/layout/activity_image_choose.xml new file mode 100644 index 00000000..ec50dd45 --- /dev/null +++ b/app/src/main/res/layout/activity_image_choose.xml @@ -0,0 +1,29 @@ + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item/layout/item_image_sp.xml b/app/src/main/res/layout/item/layout/item_image_sp.xml new file mode 100644 index 00000000..36f2e222 --- /dev/null +++ b/app/src/main/res/layout/item/layout/item_image_sp.xml @@ -0,0 +1,19 @@ + + + + + + + \ No newline at end of file