Compressing an Image before Uploading it to Firebase Storage

后端 未结 3 562
旧时难觅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:06

    I have done this way:

    private void startPosting() {
    
            mProgress.setMessage(getString(R.string.downloading_route));
    
            final String titleVal  = mRouteTitle.getText().toString().trim();
            final String descVal  = mRouteDesc.getText().toString().trim();
    
            if (!TextUtils.isEmpty(titleVal) && !TextUtils.isEmpty(descVal) && mImageUri != null) {
    
                mProgress.show();
    
                StorageReference filepath = mStorage.child("Route images").child(mImageUri.getLastPathSegment());
    
                //compress image
                mSelectImage.setDrawingCacheEnabled(true);
                mSelectImage.buildDrawingCache();
                Bitmap bitmap = mSelectImage.getDrawingCache();
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 50, byteArrayOutputStream);
                byte[] data = byteArrayOutputStream.toByteArray();
    
                UploadTask uploadTask = filepath.putBytes(data);
    
                uploadTask.addOnSuccessListener(new OnSuccessListener() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
    
                        @SuppressWarnings("VisibleForTests")
                        final Uri downloadUrl = taskSnapshot.getDownloadUrl();
    
                        final DatabaseReference newPost = mDatabase.push();
    
                        mDatabaseUser.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
    
                                newPost.child("title").setValue(titleVal);
                                newPost.child("desc").setValue(descVal);
                                newPost.child("image").setValue(downloadUrl.toString());
                                newPost.child("uid").setValue(mCurrentUser.getUid());
                                newPost.child("username").setValue(dataSnapshot.child("name").getValue()).addOnCompleteListener(new OnCompleteListener() {
                                    @Override
                                    public void onComplete(@NonNull Task task) {
    
                                        startActivity(new Intent(AddRouteActivity.this, MainActivity.class));
    
                                    }
                                });
    
                            }
    
                            @Override
                            public void onCancelled(DatabaseError databaseError) {
    
    
    
                            }
                        });
    
                        mProgress.dismiss();
    
                    }
                });
    
            }
        }
    

提交回复
热议问题