来自getDownloadUrl()的URL错误

时间:2019-09-20 15:30:46

标签: android firebase firebase-storage glide

我知道问题出在此代码上:

final String downloadUri = task.getResult().getStorage().getDownloadUrl().toString();

我的图片网址变为com.google.android.gms.tasks.zzu@42a1d558

我已经回答了所有其他问题,但是我真的不了解如何解决此问题。我是Android Studio的新手。

这是我的Firebase存储上的结果:

Here is the result on my firebase storage

这是我的代码:

    newPostBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            final String desc = newPostDesc.getText().toString();
            final String title=newPostTitle.getText().toString();

            if(!TextUtils.isEmpty(desc) && !TextUtils.isEmpty(title) && postImageUri != null){

                newPostProgress.setVisibility(View.VISIBLE);

                final String randomName = UUID.randomUUID().toString();

                // PHOTO UPLOAD
                File newImageFile = new File(postImageUri.getPath());
                try {

                    compressedImageFile = new Compressor(NewPostActivity.this)
                            .setMaxHeight(720)
                            .setMaxWidth(720)
                            .setQuality(50)
                            .compressToBitmap(newImageFile);

                } catch (IOException e) {
                    e.printStackTrace();
                }



                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                compressedImageFile.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                byte[] imageData = baos.toByteArray();

                // PHOTO UPLOAD

                final UploadTask filePath = storageReference.child("post_images").child(randomName + ".jpg").putBytes(imageData);

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

                        final String downloadUri = task.getResult().getStorage().getDownloadUrl().toString();

                        if(task.isSuccessful()){

                            File newThumbFile = new File(postImageUri.getPath());
                            try {

                                compressedImageFile = new Compressor(NewPostActivity.this)
                                        .setMaxHeight(100)
                                        .setMaxWidth(100)
                                        .setQuality(1)
                                        .compressToBitmap(newThumbFile);

                            } catch (IOException e) {
                                e.printStackTrace();
                            }



                                    Map<String, Object> postMap = new HashMap<>();
                                    postMap.put("image_url", downloadUri);
                                    postMap.put("title",title);
                                    postMap.put("desc", desc);
                                    postMap.put("user_id", current_user_id);
                                    postMap.put("timestamp", FieldValue.serverTimestamp());

                                    firebaseFirestore.collection("Posts").add(postMap).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
                                        @Override
                                        public void onComplete(@NonNull Task<DocumentReference> task) {

                                            if(task.isSuccessful()){

                                                Toast.makeText(NewPostActivity.this, "Post was added", Toast.LENGTH_LONG).show();
                                                Intent mainIntent = new Intent(NewPostActivity.this, Information.class);
                                                startActivity(mainIntent);
                                                finish();

                                            } else {


                                            }

                                            newPostProgress.setVisibility(View.INVISIBLE);

                                        }
                                    });


                        } else {

                            newPostProgress.setVisibility(View.INVISIBLE);

                        }

                    }
                });


            }

        }
    });

0 个答案:

没有答案