【问题标题】:Resize image URI to 100k将图像 URI 大小调整为 100k
【发布时间】:2016-12-24 07:59:39
【问题描述】:

我正在从 URI 中检索图像。但是加载时间太长了。我的平均文件大小是 500k。如何将其减少到 100k:

// Here is the code where I am displaying image URI

// The retrieval of URI using Picasso

 public void setImage(final Context context, final String image) {
     final ImageView postImage = (ImageView) mView.findViewById(R.id.allPostImage);
        with(context).load(image).networkPolicy(NetworkPolicy.OFFLINE).into(postImage, new Callback() {
            @Override
            public void onSuccess() {
            }

            @Override
            public void onError() {
                    Picasso.with(context).load(image).into(postImage);
            }
        });
    }

【问题讨论】:

标签: android android-imageview android-image


【解决方案1】:
Uri imageUri = imageReturnedIntent.getData();

    InputStream imageStream = null;
    try {
        imageStream = getContentResolver().openInputStream(
                imageUri);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    Bitmap bmp = BitmapFactory.decodeStream(imageStream);

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 50, stream);
    imageview.setImageBitmap(bmp);
    byte[] byteArray = stream.toByteArray();
    try {
        stream.close();
        stream = null;
    } catch (IOException e) {

        e.printStackTrace();
    }

使用它来压缩您的图像大小。

【讨论】:

  • png 文件的大小会比原始的 jpg 文件大。位图不受影响且为原始大小。
  • 将 png 替换为 jpeg,将 100 替换为 50
  • 这不会改变位图的任何内容。为什么要加载 jpg,转换为位图并再次转换为 jpg?对那个字节数组什么都不做? 50 会生成较小的 jpg 文件,质量较差,但位图仍然相同。
  • 也将 100 改为 50,它会减小尺寸。
猜你喜欢
  • 2011-03-05
  • 2017-05-07
  • 2019-04-18
  • 2013-08-12
  • 1970-01-01
  • 1970-01-01
  • 2015-10-03
  • 2019-05-21
  • 1970-01-01
相关资源
最近更新 更多