Mehul Kanzariya
Mehul Kanzariya

Reputation: 1348

Unable to show images from Firebase Storage

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);
    }

Upvotes: 2

Views: 4341

Answers (1)

Amitai Fensterheim
Amitai Fensterheim

Reputation: 841

In order to show images you should use ImageView.

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.

like

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

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

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

See this answer.

Upvotes: 3

Related Questions