• Android 图片上传


        implementation 'com.github.LuckSiege.PictureSelector:picture_library:v2.2.4'
    
    • 1

    使用

    public class MainActivity extends AppCompatActivity {
    
        ActivityMainBinding binding;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            binding=ActivityMainBinding.inflate(getLayoutInflater());
            setContentView(binding.getRoot());
    
            binding.btnPhoto.setOnClickListener(v -> {
                //拍照
                PictureSelector.create(MainActivity.this)
                        .openCamera(PictureMimeType.ofImage())
                        .forResult(PictureConfig.CHOOSE_REQUEST);
            });
    
            binding.btnPic.setOnClickListener(v -> {
                //相册
                PictureSelector.create(this)
                        .openGallery(PictureMimeType.ofImage())
                        .maxSelectNum(1)
                        .minSelectNum(1)
                        .imageSpanCount(4)
                        .selectionMode(PictureConfig.MULTIPLE)
                        .forResult(PictureConfig.CHOOSE_REQUEST);
    
            });
    
        }
    
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
                switch (requestCode) {
                    case PictureConfig.CHOOSE_REQUEST:
                        // 图片选择结果回调
                        List<LocalMedia> images= PictureSelector.obtainMultipleResult(data);
                        for (LocalMedia image : images) {
                            String path = image.getPath();
                            Bitmap bitmap = BitmapUtil.getSmallBitmap(path);
                            binding.img.setImageBitmap(bitmap);
                        }
                        break;
                }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    Bitmap 工具类

    public class BitmapUtil {
    
        public static String compressImage(String filePath) {
    
            //原文件
            File oldFile = new File(filePath);
    
            //压缩文件路径 照片路径/
            String targetPath = oldFile.getPath();
            int quality = 60;//压缩比例0-100
            Bitmap bm = getSmallBitmap(filePath);//获取一定尺寸的图片
    
            File outputFile = new File(targetPath);
            try {
                if (!outputFile.exists()) {
                    outputFile.getParentFile().mkdirs();
                    //outputFile.createNewFile();
                } else {
    //                outputFile.delete();//需要注释否则 会删除 相册 原有的图片
                }
                FileOutputStream out = new FileOutputStream(outputFile);
                bm.compress(Bitmap.CompressFormat.JPEG, quality, out);
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
                return filePath;
            }
            return outputFile.getPath();
        }
    
        /**
         * 根据路径获得图片信息并按比例压缩,返回bitmap
         */
        public static Bitmap getSmallBitmap(String filePath) {
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;//只解析图片边沿,获取宽高
            BitmapFactory.decodeFile(filePath, options);
            // 计算缩放比
            options.inSampleSize = calculateInSampleSize(options, 1080, 1920);
            // 完整解析图片返回bitmap
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeFile(filePath, options);
        }
    
        // 计算缩放比
        public static int calculateInSampleSize(BitmapFactory.Options options,
                                                int reqWidth, int reqHeight) {
            final int height = options.outHeight;
            final int width = options.outWidth;
            int inSampleSize = 1;
            if (height > reqHeight || width > reqWidth) {
                final int heightRatio = Math.round((float) height / (float) reqHeight);
                final int widthRatio = Math.round((float) width / (float) reqWidth);
                inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
            }
            return inSampleSize;
        }
        public static Bitmap convertViewToBitmap(View view) {
    
            view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    
            view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
    
            view.buildDrawingCache();
    
            Bitmap bitmap = view.getDrawingCache();
    
            return bitmap;
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
  • 相关阅读:
    计算机毕设(附源码)JAVA-SSM基于疫情的智慧社区管理系统
    【 Vue 路由 跳转 路由守卫 】
    AndroidStudio案例——跑马灯
    【数字IC验证快速入门】11、Verilog TestBench(VTB)入门
    【swift】struct与class 的区别
    P3205 [HNOI2010]合唱队
    squidpy 安装 pip install SpatialDE -i https://pypi.tuna.tsinghua.edu.cn/simple spatial
    使用Vite快速构建Vue3+ts+pinia脚手架
    Spring:常用注解总结(持续更新~~~)
    LeetCode234(Python)—— 回文链表(简单)
  • 原文地址:https://blog.csdn.net/weixin_37077736/article/details/126675044