无法在Firebase中将压缩图像作为缩略图上传

时间:2018-08-09 06:53:58

标签: firebase image-processing android-studio-3.0

我试图将压缩的图像上传到我的Firebase数据库中以将其显示为缩略图,然后再插入图像压缩代码,我的个人资料图片会正确显示,但在插入之后,我的代码只能在个人资料所在的部分使用图像已上传,但涉及到拇指部分时,仅显示进度条,不显示成功吐司或失败吐司。下面是代码。

  mImageStorage = FirebaseStorage.getInstance().getReference();


    mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
    String uuid = mCurrentUser.getUid();

    mUserRef = FirebaseDatabase.getInstance().getReference().child("Users").child(uuid);

以上部分是针对Firebase初始化的。

 mImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           Intent galleryIntent = new Intent();
            galleryIntent.setType("image/*");
            galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(galleryIntent, "SELECT IMAGE"), GALLERY_PICK);
            // start picker to get image for cropping and then use the image in cropping activity
           /* CropImage.activity()
                    .setGuidelines(CropImageView.Guidelines.ON)
                    .start(SettingActivity.this);*/
        }
    });

按钮相机侦听器。

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode==GALLERY_PICK&&resultCode==RESULT_OK){
        Uri imageuri = data.getData();
        CropImage.activity(imageuri).setAspectRatio(1,1)
                .setMinCropWindowSize(500,500)
                .start(this);
    }
    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        if (resultCode==RESULT_OK){

            mProgess = new ProgressDialog(SettingActivity.this);
            mProgess.setTitle("Uploading Image....");
            mProgess.setMessage("Please wait while we update the image");
            mProgess.setCanceledOnTouchOutside(false);
            mProgess.show();
            Uri resultUri=result.getUri();

            File thumb_filePath = new File(resultUri.getPath());

           final String current_user_id = mCurrentUser.getUid();

            Bitmap thumb_bitmap = new Compressor(this).setMaxWidth(200).setMaxHeight(200)
                    .setQuality(75)
                    .compressToBitmap
                    (thumb_filePath);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            thumb_bitmap.compress(Bitmap.CompressFormat.JPEG,100,baos);
            final byte[] mData = baos.toByteArray();

            StorageReference filePath= mImageStorage.child("profile_images").child
                    (current_user_id+".jpg");
            final  StorageReference thumb_file = mImageStorage.child("profile_images").child
                    ("thumbs").child(current_user_id+"jpg");

            filePath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                    if (task.isSuccessful()){

                        mImageStorage.child("image").child(current_user_id+".jpg")
                                .getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {

                            @Override
                            public void onSuccess(Uri uri) {

                                final String downloadUrl = uri.toString();

                                UploadTask upload = thumb_file.putBytes(mData);
                                upload.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                                    @Override
                                    public void onComplete(@NonNull final Task<UploadTask
                                            .TaskSnapshot> thumb_task) {

                                        mImageStorage.child("image").child
                                                (current_user_id + "" +
                                                        ".jpg")
                                                .getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {

                                            @Override
                                            public void onSuccess(Uri uri) {

                                               final String thumbUrl = uri.toString();

                                                if (thumb_task.isSuccessful()) {

                                                    Map update_hash = new HashMap();
                                                    update_hash.put("image",downloadUrl);
                                                    update_hash.put("thumbnail",thumbUrl);
                                                    mUserRef.updateChildren(update_hash).addOnCompleteListener(new OnCompleteListener<Void>() {
                                                                @Override
                                                                public void onComplete(@NonNull Task<Void> task) {
                                                                    if (task.isSuccessful()) {
                                                                        mProgess.dismiss();
                                                                        Toast.makeText(SettingActivity.this,
                                                                                "Profile picture updated", Toast
                                                                                        .LENGTH_SHORT)
                                                                                .show();
                                                                    }
                                                                }
                                                            });


                                                } else {
                                                    mProgess.dismiss();
                                                    Toast.makeText(SettingActivity.this,
                                                            "Unsuccessful in uploading thumb", Toast
                                                                    .LENGTH_SHORT)
                                                            .show();

                                                }

                                            }
                                        });


                                    }
                                });
                            }
                        });

                    }else{
                        Toast.makeText(SettingActivity.this,"Unsuccessful",Toast.LENGTH_SHORT)
                                .show();
                        mProgess.dismiss();
                    }
                }
            });

        }else if (resultCode==CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE){
            Exception error = result.getError();
        }
    }
}

此位用于图像上传,谢谢!

0 个答案:

没有答案