将图像上传到Firebase中的其他文件夹

问题描述 投票:0回答:1

我正在创建一个应用程序,用户可以上传两种类型的图像,即个人资料图片和个人资料封面图片。但由于无法调用两个onActivityResult,我遇到了定义不同过程而又彼此不干扰的问题。

这是我当前用于将个人资料图片上传到Firebase存储的代码

  coverbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent galleryIntent = new Intent();
            galleryIntent.setType("image/*");
            galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

            startActivityForResult(Intent.createChooser(galleryIntent, "SELECT IMAGE"), GALLERY_PICKER);
        }
    });

    mImagebtn.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);

        }
    });
}

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

    if (requestCode == GALLERY_PICK && resultCode == RESULT_OK) {

        mPdialog = new ProgressDialog(SettingsActivity.this);
        mPdialog.setTitle("Uploading");
        mPdialog.setMessage("please wait....");
        mPdialog.show();

        Uri imageUri = data.getData();

        CropImage.activity(imageUri)
                .setAspectRatio(1, 1)
                .setMinCropWindowSize(500,500)
                .start(SettingsActivity.this);

    }

    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        if (resultCode == RESULT_OK) {
            Uri resultUri = result.getUri();

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

            String current_uid = mCurrentuser.getUid();

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

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

            StorageReference filepath = mImageReference.child("profile_images").child(current_uid + ".jpg");
            final StorageReference thumb_path = mImageReference.child("profile_images").child("thumbs").child(current_uid + ".jpg");
            filepath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {

                    if (task.isSuccessful()){

                        final String download_url = task.getResult().getDownloadUrl().toString();
                        UploadTask uploadTask = thumb_path.putBytes(thumb_byte);
                        uploadTask.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                            @Override
                            public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> thumb_task) {

                                String thumb_downloadurl = thumb_task.getResult().getDownloadUrl().toString();

                                if (thumb_task.isSuccessful()){

                                    Map update_hashmap = new HashMap();
                                    update_hashmap.put("image",download_url);
                                    update_hashmap.put("thumb_image", thumb_downloadurl);

                                    mDatabase.updateChildren(update_hashmap).addOnCompleteListener(new OnCompleteListener<Void>() {
                                        @Override
                                        public void onComplete(@NonNull Task<Void> task) {

                                            if (task.isSuccessful()){
                                                mPdialog.dismiss();
                                                Toast.makeText(SettingsActivity.this,"success",Toast.LENGTH_LONG).show();

                                            }
                                        }
                                    });
                                }else {
                                    Toast.makeText(SettingsActivity.this,"failed",Toast.LENGTH_LONG).show();
                                    mPdialog.dismiss();
                                }
                            }
                        });


                        }else {
                        Toast.makeText(SettingsActivity.this,"failed",Toast.LENGTH_LONG).show();
                        mPdialog.dismiss();

                    }
                }
            });

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

我正在创建一个应用程序,用户可以上传两种类型的图像,即个人资料图片和个人资料封面图片。但是我在定义不带...

java android firebase firebase-storage
1个回答
0
投票

尝试一下将startActivityForResult和intent移到外面根据点击的按钮设置意图requestCode

© www.soinside.com 2019 - 2024. All rights reserved.