【问题标题】:Image picker upload to firestore and display图像选择器上传到 Firestore 并显示
【发布时间】:2019-06-12 23:53:42
【问题描述】:

我有一个活动,我将其转换为片段。

代码:

    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 当我点击(requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)IMAGE_ACTIVITY_REQUEST_CODE 等于203 那里没有通过if 的条件陈述。这段代码在活动中运行良好,所以我很困惑为什么它现在不起作用

【问题讨论】:

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


    【解决方案1】:

    通过替换解决了问题

    .start(Objects.requireNonNull(getActivity()));
    

    .start(getContext(), this);
    

    这个github 帖子更详细地解释了它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-21
      • 2018-11-01
      • 2012-07-07
      • 2020-06-24
      • 1970-01-01
      • 1970-01-01
      • 2014-09-03
      • 2023-01-04
      相关资源
      最近更新 更多