图像选择器上传到Firestore并显示

时间:2019-06-12 23:53:42

标签: android firebase google-cloud-firestore firebase-storage

我有一个活动,然后转换为片段。

代码:

    mImageBtn.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_PICK);

        }
    });



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

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

        String imageUri = data.getDataString();

        CropImage.activity(Uri.parse(imageUri))
                .setAspectRatio(1, 1)
                .start(Objects.requireNonNull(getActivity()));

    }

    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);

            mProgressDialog = new ProgressDialog((getActivity()));
            mProgressDialog.setTitle("Uploading");
            mProgressDialog.setMessage("Pleas Stand By");
            mProgressDialog.setCanceledOnTouchOutside(false);
            mProgressDialog.show();

            Uri resultUri = result.getUri();

            final String current_user_id = mCurrentUser.getUid();

            final StorageReference filepath = mImageStorage.child("profile_images").child(current_user_id + (".jpeg"));
            filepath.putFile(resultUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                    filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {

                            Map<String, String> newImageUrl = new HashMap<>();
                            newImageUrl.put("image", filepath.toString());

                            db.collection("Users").document(current_user_id)
                                    .set(newImageUrl, SetOptions.merge())
                                    .addOnSuccessListener(new OnSuccessListener<Void>() {
                                        @Override
                                        public void onSuccess(Void aVoid) {
                                            Log.d(TAG, "Image uploaded!");
                                            mProgressDialog.dismiss();
                                            //Toast.makeText(user_profile.this, "Succesful Upload", Toast.LENGTH_LONG).show();

                                            filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                                                @Override
                                                public void onSuccess(Uri uri) {

                                                    //The download url
                                                    final String downloadUrl =
                                                            uri.toString();
                                                    Log.d("tag", downloadUrl);
                                                    if (!downloadUrl.equals("default")) {
                                                        // I changed this to glide since i thought picasso was the problem.
                                                        // Picasso still should work. Glide is recommended by google tho
                                                        Glide.with(Objects.requireNonNull(getContext())).load(downloadUrl).into(mDisplayImage);
                                                    }

                                                }
                                            })
                                                    .addOnFailureListener(new OnFailureListener() {
                                                        @Override
                                                        public void onFailure(@NonNull Exception e) {
                                                            Log.w(TAG, "Error writing document", e);
                                                         //   Toast.makeText(getApplicationContext(), "There was some error in saving Changes.", Toast.LENGTH_LONG).show();
                                                        }
                                                    });

此代码试图做的是调出图像选择器,允许用户将图像上传到Firestore,然后将图像显示在imageview中

该应用程序不会崩溃,但没有任何代码经过此行if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)

当我单击result code = 1Request code = -1等于203时,我在调试器中玩转,尝试诊断发现(requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)IMAGE_ACTIVITY_REQUEST_CODE的问题不通过if语句的条件。该代码在活动中运行良好,因此对为什么现在不起作用感到非常困惑

1 个答案:

答案 0 :(得分:0)

通过替换

解决了该问题
.start(Objects.requireNonNull(getActivity()));

使用

.start(getContext(), this);

github帖子对其进行了更详细的说明