【问题标题】:How can I compress the file size and quality when uploading pictures?上传图片时如何压缩文件大小和质量?
【发布时间】:2023-03-31 15:16:02
【问题描述】:

上传图片时如何压缩文件大小和质量?

我已经完成了上传文件到firebase,如何压缩文件?

因为文件很大时,在recyclerview中加载会变慢。

所以我想压缩文件。

 Uri imageUri = imageListUri.get(index);
        if(spinner.getSelectedItem().toString().equals(getApplicationContext().getResources().getString(R.string.choose_sub))) {
            AlertDialog alertDialog = new AlertDialog.Builder(this)
                    .setTitle(getApplicationContext().getResources().getString(R.string.alert_error))
                    .setMessage(getApplicationContext().getResources().getString(R.string.choose_sub))
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).create();
            alertDialog.show();
        } else if(image_check.equals("ok")) {
            final ProgressDialog progressDialog = new ProgressDialog(this);
            progressDialog.setMessage(getApplicationContext().getResources().getString(R.string.posting));
            progressDialog.show();

            if (imageUri != null) {
                final StorageReference filerefrence = storageReference.child(System.currentTimeMillis()
                        + "." + getFileExtension(imageUri));
                // scaling the image
                int scaleDivider = 1;
                try {
                    Bitmap fullBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
                    int scaleWidth = fullBitmap.getWidth() / scaleDivider;
                    int scaleHeight = fullBitmap.getHeight() / scaleDivider;
                    byte[] downsizedImageBytes =
                            getDownsizedImageBytes(fullBitmap, scaleWidth, scaleHeight);

                    uploadTask = filerefrence.putBytes(downsizedImageBytes);
                } catch (IOException e) {
                    e.printStackTrace();
                }
static public byte[] getDownsizedImageBytes(Bitmap fullBitmap, int scaleWidth, int scaleHeight) throws IOException {

        Bitmap scaledBitmap = Bitmap.createScaledBitmap(fullBitmap, scaleWidth, scaleHeight, true);

        // 2. Instantiate the downsized image content as a byte[]
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

        return baos.toByteArray();
    }

那么,对吗?

try {
                    Bitmap fullBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    fullBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out); // Here 100 is the quality in percent of the image
                    Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));

                    int scaleWidth = fullBitmap.getWidth() / scaleDivider;
                    int scaleHeight = fullBitmap.getHeight() / scaleDivider;
                    byte[] downsizedImageBytes =
                            getDownsizedImageBytes(fullBitmap, scaleWidth, scaleHeight);

                    uploadTask = filerefrence.putBytes(downsizedImageBytes);
                } catch (IOException e) {
                    e.printStackTrace();
                }

【问题讨论】:

    标签: android android-bitmap


    【解决方案1】:

    您可以像这样使用BitmapFactory 类来做到这一点

    Bitmap original = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri)
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    original.compress(Bitmap.CompressFormat.JPEG, 100, out); // Here 100 is the quality in percent of the image
    Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
    
    

    请注意,PNG 已经是一种无损格式,因此在压缩时不会有任何区别,如果您真的想看到使用 JPEG 的效果

    质量接受 0 - 100

    0 = 最大压缩(适用于小图像的最低质量)

    100 = 最小压缩(适用于大图像的 MAX 质量)

    感谢this 回答

    【讨论】:

    • 谢谢,但是如何在数组中使用?因为我用过 byte[] downsizedImageBytes
    • 可以显示getDownsizedImageBytes(fullBitmap, scaleWidth,scaleHeight);方法的内容吗?它已经返回一个字节数组,您可以直接在那里应用您的转换,然后返回一个字节数组
    • 我认为你做得对,而不是100你可以指定你想要压缩到的质量
    • 可是,我还是一头雾水,怎么办?我只是添加 ByteArrayOutputStream out = new ByteArrayOutputStream(); original.compress(Bitmap.CompressFormat.JPEG, 100, out);位图解码 = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));这三行?
    • 对就是这样,压缩后要返回位图的字节数组