【问题标题】:How to reduce size (mb) of image picked from gallery [duplicate]如何减小从图库中挑选的图像的大小(mb)[重复]
【发布时间】:2019-05-27 03:59:54
【问题描述】:

当用户从图库中选择图像时,我将其转换为 base64 字符串。然后我使用 API。图片大小必须小于 2MB,所以如果图库中的图片大于 2MB,我需要减小它的大小。 我该怎么做?

这是我的代码:

// I pass the URI of the image to the method
@Override
    protected Integer doInBackground(Uri... uris) {

        InputStream inputStream = null;
        try {
            inputStream = getContentResolver().openInputStream(uris[0]);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        byte[] buffer = new byte[8192];
        int bytesRead;
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        try {
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                output.write(buffer, 0, bytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        bytes = output.toByteArray();
        String encodedImage = Base64.encodeToString(bytes, Base64.NO_WRAP);

【问题讨论】:

    标签: java android


    【解决方案1】:

    根据开发人员文档,一种轻松检查和存储图像的方法 https://developer.android.com/topic/performance/graphics/load-bitmap#java 这为您的答案提供了正确解释的解决方案,另一种方法是更改​​可以在此处找到的图像的大小,然后相应地使用它 https://developer.android.com/reference/android/graphics/drawable/ScaleDrawable

    【讨论】:

      【解决方案2】:

      您可以使用创建位图对象

      BitmapFactory.decodeByteArray

      从指定的字节数组中解码一个不可变位图。

      然后你就可以使用了

      bitmap.compress

      将位图的压缩版本写入指定 输出流。如果返回 true,则可以通过以下方式重建位图 将相应的输入流传递给 BitmapFactory.decodeStream()。 注意:并非所有格式都直接支持所有位图配置,所以它是 从 BitmapFactory 返回的位图可能在 不同的位深度,和/或可能丢失了每像素的 alpha(例如 JPEG 仅支持不透明像素)。

      此方法可能需要几秒钟才能完成,所以它应该只 从工作线程调用。

      和/或

      Bitmap.createScaledBitmap

      在可能的情况下,创建一个从现有位图缩放的新位图。 如果指定的宽高和当前宽一样 和源位图的高度,返回源位图并且没有 新位图已创建。

      在我的项目中,我创建了两个实用函数,可以帮助您了解如何使用我上面提到的函数来满足您的需求

      public static Bitmap base64ToBitmap(String encodedImage) {
          byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
          return BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
      }
      
      public static String bitmapToBase64(Bitmap bitmap, int quality, int percentScale) {
          if (quality < 0 || quality > 100) {
              quality = IMAGE_QUALITY_DEFAULT_VALUE;
          }
          if (percentScale < 0 || percentScale > 100) {
              percentScale = IMAGE_SCALE_DEFAULT_VALUE;
          }
      
          float scale = (float) percentScale / 100;
          int width = Math.round(bitmap.getWidth() * scale);
          int height = Math.round(bitmap.getHeight() * scale);
          bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
      
          ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
          bitmap.compress(Bitmap.CompressFormat.JPEG, quality, byteArrayOutputStream);
          byte[] byteArray = byteArrayOutputStream.toByteArray();
          return Base64.encodeToString(byteArray, Base64.NO_WRAP);
      }
      

      【讨论】:

        猜你喜欢
        • 2018-03-06
        • 1970-01-01
        • 1970-01-01
        • 2012-08-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-30
        • 1970-01-01
        相关资源
        最近更新 更多