Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display images from Google Drive using NetworkImage

Tags:

flutter

I am trying to load some images into a gridview. Each GridTile contains a container with a decoration that uses a NetworkImage. All the images I'm using are stored in google drive and the problem is that for some reason, flutter does not like Google Drive urls. I've tried the following urls:

  • https://docs.google.com/uc?id={fileId}
  • https://drive.google.com/uc?export=view&id={fileId}
  • https://drive.google.com/a/{domain}.com/uc?authuser=0&id=fileId}&export=download
  • https://drive.google.com/file/d/{fileId}/view

None of the above is working. It throws the following error:

The following _Exception was thrown resolving an image codec: Exception: Could not instantiate image codec.

What I've noticed is that all of those urls end up redirecting to another link, probably where the image is cached. Is that the reason why is not working? If that is so, how can I force it to follow redirects?

This is the relevant piece of code:

String imgUrl = "https://drive.google.com/file/d/$fileId/view";
GridTile(
    child: InkWell(
      onTap: () {
        openDriveImage(fileId);
      },
      child: Container(
        margin: EdgeInsets.all(5.0),
        decoration: BoxDecoration(
          border: Border.all(
            color: Colors.black38,
          ),
          shape: BoxShape.rectangle,
          image: DecorationImage(
            fit: BoxFit.fitWidth,
            image: NetworkImage(imgUrl),
          ),
        ),
      ),
    ),
like image 606
Morfinismo Avatar asked Jan 21 '20 21:01

Morfinismo


People also ask

How do I use a Google Drive image in HTML and on a website?

How Embed Works? Go to drive.google.com, right-click any image file, and choose Get Link to get a shareable link of that image. Make sure that the access is set to Anyone on the internet with this link can view. Paste that file link in the box below to generate the HTML embed code.

Can you embed Google Drive images?

You upload an image file to Google Drive and make that file public. Google Drive will now generate a high-resolution thumbnail image of the uploaded file that you can directly embed on your website or emails.

How do I get a JPEG URL from Google Drive?

1. Right click on the selected image in Google Drive 2. Select “Get Shareable link” 3. The link of the image is copied.


1 Answers

Here are the steps:

  1. Right click the image and click Share as shown below;enter image description here

  2. In the dialog that appears, make sure the option selected from the dropdown is Anyone with the link can view as shown below: enter image description here

  3. After, click Copy link and then paste it somewhere

  4. It should be something like this https://drive.google.com/file/d/1VdjEgb0aZl9IZa2jOzGU5_SNbmlmeiCj/view?usp=sharing which is in the format

    https://drive.google.com/file/d/<FILE_ID>/view?usp=sharing

  5. Copy the <FILE_ID> which in my case is 1VdjEgb0aZl9IZa2jOzGU5_SNbmlmeiCj

  6. Append it to this link below in the following format:

    https://drive.google.com/uc?export=view&id=<FILE_ID>

  7. In my case for example, the final link is:

    https://drive.google.com/uc?export=view&id=1VdjEgb0aZl9IZa2jOzGU5_SNbmlmeiCj

  8. Finally get that link and paste in a NetworkImage() or Image.network() widget and your done
like image 129
Peter Wauyo Avatar answered Sep 22 '22 03:09

Peter Wauyo