无法显示Firebase存储中的图像 [英] Unable to show images from Firebase Storage

查看:70
本文介绍了无法显示Firebase存储中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用新的Firebase存储并上传图像,然后获取下载URL.将下载网址转换为字符串后,我正在使用它显示在我的活动中,但它仅显示下载网址. 我正在使用以下代码上传图片并获取下载网址:

I am using new Firebase Storage and uploading image and then getting the download url. After converting the download url to string, i am using it to display in my activity but it just shows download url. I am using below code to upload image and get download url :

    dialogBuilder.setTitle("Add Shop");
    dialogBuilder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            //do something with edt.getText().toString();

            Uri image_uri = getImageUri();
            enteredShopName = shopName.getText().toString();
            enteredShopDescription = shopDescription.getText().toString();

            FirebaseStorage storage = FirebaseStorage.getInstance();
            // Create a storage reference from our app
            StorageReference storageRef = storage.getReferenceFromUrl("gs://myapp.com");
            StorageReference imagesRef = storageRef.child("shop_images");
            StorageReference selectimage_ref = imagesRef.child(image_uri.getLastPathSegment());

            // upload file to firebase storage
            selectimage_ref.putFile(image_uri).
            addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    Uri download_url = taskSnapshot.getDownloadUrl();
                    String string_download_url = download_url.toString();
                    Shop shop = new Shop(enteredShopName, enteredShopDescription,string_download_url);
                    listRef.push().setValue(shop, new DatabaseReference.CompletionListener() {
                        @Override
                        public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
                            if (databaseError != null) {
                                Log.e(TAG, "Failed to write message", databaseError.toException());
                            } else {
                                Toast.makeText(MainActivity.this, "Shop added successfully", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });

                }
            });

获取网址后,我将其设置为如图所示:

After getting url i am setting it as shown to show image :

        public void setImg_url(String string_download_url){
        TextView field = (TextView) mView.findViewById(R.id.tv_shopImg);
        field.setText(string_download_url);
    }

推荐答案

要显示图像,您应该使用 ImageView .

In order to show images you should use ImageView.

您可以使用 Picasso 库在 ImageView 中显示图像.您只需要通过图像视图将url,uri或资源传递给毕加索即可.

You can use Picasso Library to show images in ImageView. You just need to pass the url, uri or resource to Picasso with image view.

喜欢

Picasso.with(this).load(/* url of image */).into(/*your imageview id*/);

要使用毕加索,您需要在Gradle中添加以下内容

To use the picasso you need to add the following in Gradle

compile 'com.squareup.picasso:picasso:2.5.2'

查看此答案.

这篇关于无法显示Firebase存储中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆