【问题标题】:Cant load picture from Firebase Storage to an Imageview无法将图片从 Firebase 存储加载到 Imageview
【发布时间】:2019-12-25 20:44:13
【问题描述】:

我正在我的应用中将图片上传到 Firebase 存储。它可以很好地上传,并且可以在 firebase 存储中在线查看。当我试图获取它并在图像视图中显示它时,它不会显示在图像视图中,而是图像视图也会消失。我已经用 Piccaso 和 glide 尝试过这个,但没有任何效果。下面附上代码

上传代码:

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

        if (requestCode == PICK_IMAGE && resultCode == RESULT_OK && data != null && data.getData() != null) {

            uri = data.getData();

            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);




                profile_image = findViewById(R.id.profileimage);
                profile_image.setImageBitmap(bitmap);



                imageRef=storageReference.child(userID);
                imageRef.putFile(uri)
                        .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                            @Override
                            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                                //if the upload is successful
                                //hiding the progress dialog
                                //and displaying a success toast
                                //dismissDialog();

                                profilePicUrl = taskSnapshot.getMetadata().getReference().getDownloadUrl().toString();
                            }
                        })
                        .addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception exception) {
                                //if the upload is not successful
                                //hiding the progress dialog
                                // dismissDialog();
                                //and displaying error message
                                Toast.makeText(profile.this, exception.getCause().getLocalizedMessage(), Toast.LENGTH_LONG).show();
                            }


                        });















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

下载并加载到图像视图代码:


        fbstorage=FirebaseStorage.getInstance();
        storageReference=fbstorage.getReference();
        imageRef=storageReference.child(userID);
        final String h=imageRef.getDownloadUrl().toString();

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

                Toast.makeText(profile.this, "hello", Toast.LENGTH_SHORT).show();
                Picasso.with(profile.this).load(h).into(profile_image);

            }
        });

【问题讨论】:

  • 上传文件时是否尝试将图像设置为onSuccess()内部的视图?
  • @AlexMamo 是的,同样的问题
  • 你为什么加载h而不是你刚刚检索到的uri?

标签: java android firebase firebase-storage


【解决方案1】:

用 glide 试试 .. 这个代码很适合我

  StorageReference storageReference = FirebaseStorage.getInstance().getReference().child("folderName"+"/"+thePicName);
    Glide.with(context.getApplicationContext())
            .using(new FirebaseImageLoader())
            .load(storageReference)
            .asBitmap()
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .priority(Priority.IMMEDIATE)
            .into(new BitmapImageViewTarget(pic) {
                @Override
                protected void setResource(Bitmap resource) {
                    RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(context.getResources(),
                            Bitmap.createScaledBitmap(resource, 150, 150, false));
                    drawable.setCircular(false);
                    pic.setImageDrawable(drawable);
                }
            });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-16
    • 1970-01-01
    • 2020-09-02
    • 2019-03-20
    • 2016-11-28
    • 2020-12-24
    相关资源
    最近更新 更多