Compressing an Image before Uploading it to Firebase Storage

后端 未结 3 606
旧时难觅i
旧时难觅i 2021-02-06 18:24

I am trying to Compress an Image before Uploading it to Firebase Storage using SiliCompressor library , but it seems not working , the ProgressD

3条回答
  •  一生所求
    2021-02-06 19:19

    //declear local variable first

    Bitmap bitmap; Uri imageUri;

    //button action to call Image picker method

    ImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,"Pick Image"),GALLERY_REQ_CODE);
            }
        });
    

    //get bitmap from onActivityResult

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == GALLERY_REQ_CODE && resultCode == RESULT_OK && data != null) {
            imageUri = data.getData();
            try {
                bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
            } catch (IOException e) {
                e.printStackTrace();
            }
            imageView.setImageURI(imageUri);
        }
    }
    

    //compress image first then upload to firebase

    public void postImage() {
        StorageReference storageReference = mStorageRef.child("Images/" + //imageName);
        databaseReference = FirebaseDatabase.getInstance().getReference().child("Jobs").child(//imageName);
    
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 20, bytes);
        String path = MediaStore.Images.Media.insertImage(SaveJobActivity.this.getContentResolver(),bitmap,//imageName,null);
    
        Uri uri = Uri.parse(path);
        storageReference.putFile(uri).addOnSuccessListener(new OnSuccessListener() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                taskSnapshot.getStorage().getDownloadUrl().addOnCompleteListener(new OnCompleteListener() {
                    @Override
                    public void onComplete(@NonNull Task task) {
                        final String downloadUrl = task.getResult().toString();
    
                        if (task.isSuccessful()){
                            Map update_hashMap = new HashMap<>();
    
                **//assign download url in hashmap to upadate database reference**
    
                            update_hashMap.put("Image",downloadUrl);
    
                **//update database children here**
    
                            databaseReference.updateChildren(update_hashMap).addOnCompleteListener(new OnCompleteListener() {
                                @Override
                                public void onComplete(@NonNull Task task) {
                                    if (task.isSuccessful()){
                                        //do what you want
                                    }else {
                                        //show exception
                                    }
                                }
                            });
                        }else{
                            //show exception
                        }
                    }
                });
            }
        });
    }
    

提交回复
热议问题