如何实现有效的图像压缩?

时间:2019-05-30 01:06:39

标签: android image

我想先压缩图像,然后再上传并托管在存储参考上。有什么办法可以有效地做到这一点?

即使质量不是最好的,重点还是要减小尺寸。

private void StoringImageToFirebaseStorage()
    {

        String uri = PostsImagesRefrence.child("Post Images").child(ImageUri.getLastPathSegment() + postRandomName + ".jpg").toString();

        StorageReference filePath = PostsImagesRefrence.child("Post Images").child(ImageUri.getLastPathSegment() + postRandomName + ".jpg");

        filePath.putFile(ImageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task){
                if (task.isSuccessful()){
                    downloadUrl = task.getResult().getDownloadUrl().toString();
                    SavingInformationToDatabase();
                }
                else
                {.....}
    }

private void OpenGallery() {
                Intent galleryIntent = new Intent();
                galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
                galleryIntent.setType("image/*");
                startActivityForResult(galleryIntent, Gallery_Pick);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode==Gallery_Pick && resultCode==RESULT_OK && data!=null{
            ImageUri = data.getData();
            PostImage.setImageURI(ImageUri);
            PostImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v){
                    OpenGallery();
                {...}
    }

1 个答案:

答案 0 :(得分:0)

您好,您可以使用此简单的轻量级库将图像压缩为所需的任何大小,这还将帮助您将这些图像直接以字节为单位上传到firebase。

Image Compressor

  new ImageCompressor(imageFile.getAbsolutePath(), imageFilePath, new ImageCompressListener() {
                            @Override
                            public void onImageCompressed(byte[] outputImageBaos, File imageFile) {
                                if (outputImageBaos != null) {
                                    final StorageReference mediaFileRef = mStoreRef.child("images/" + imageFilePath);
                                    currenTask = mediaFileRef.putBytes(outputImageBaos);

                                    currenTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
                                        @Override
                                        public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                                            if (!task.isSuccessful()) {
                                                throw task.getException();
                                            }

                                            // Continue with the task to get the download URL
                                            return mediaFileRef.getDownloadUrl();
                                        }
                                    }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                                        @Override
                                        public void onComplete(@NonNull Task<Uri> task) {
                                            if (task.isSuccessful()) {
                                                String downloadUri = task.getResult().toString();

                                            }
                                        }
                                    });
                                    currenTask.addOnFailureListener(new OnFailureListener() {
                                        @Override
                                        public void onFailure(@NonNull Exception e) {
                                            sendProgressBroadcast(AppConstants.UPLOAD_FAILED, -1);
                                        }
                                    }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                                        @Override
                                        public void onProgress(com.google.firebase.storage.UploadTask.TaskSnapshot taskSnapshot) {
                                            int progress = (int) ((100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount());
                                            sendProgressBroadcast(AppConstants.UPLOAD_PROGRESS, progress);
                                        }
                                    });
                                }
                            }
                        });