将图像URI调整为100k

时间:2016-12-24 07:59:39

标签: android android-imageview android-image

我正在从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);
            }
        });
    }

1 个答案:

答案 0 :(得分:0)

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();
    }

使用它来压缩图像大小。