question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

drawing images stored in the app cache

See original GitHub issue

I have been looking for a way to display images stored in the app cache downloaded from the Internet, which I saved using the Expo FileSystem.createDownloadResumable package.

Partial code:


        let url = "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ee/Catedral_de_Mar%C3%ADa_Reina_del_Mundo%2C_Montreal%2C_Canad%C3%A1%2C_2017-08-12%2C_DD_61-63_HDR.jpg/1280px-Catedral_de_Mar%C3%ADa_Reina_del_Mundo%2C_Montreal%2C_Canad%C3%A1%2C_2017-08-12%2C_DD_61-63_HDR.jpg";

        let localSRC = FileSystem.cacheDirectory + 
            Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15) +
            '.jpg';

           const downloadResumable = FileSystem.createDownloadResumable(
                url, localSRC, {}, downloadProgress => {
                    const progress = downloadProgress.totalBytesWritten / downloadProgress.totalBytesExpectedToWrite;
                    console.log(progress);
                }
            );

            const { uri } = await downloadResumable.downloadAsync();

            const image = new Image(canvas);
           // my code works here if I replace uri by url
            image.src = uri;

            image.addEventListener('load', () => {
               context.drawImage(image, 0, 0, 100, 100);
            }); 

The path to the file in Android looks like this: file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540user%252Fappname/g0ibtcg4zag2kcxoxvozr9.jpg

Question 1: is it in theory possible to load images on the canvas from a local image? The issue #60 and the example in your repo seems to suggest that it should work.

Question 2: is the path not correctly build, or are there permissions issues preventing the canvas package from accessing the cache folder above? The load event never gets fired with the local path. Should I replace the FileSystem.cacheDirectory to something else to make it work? If so, what?

I’d appreciate some guidance. I have been spending a fair bit of time trying to resolve this, but the issue above is the closest clue that I have seen.

thanks!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
VityaSchelcommented, Sep 3, 2021

readAsStringAsync

Docs says FileSystem.readAsStringAsync returns “A Promise that resolves to a string containing the entire contents of the file.” so correct code would be

  const ctx = canvasRef.current.getContext('2d')
  const path = "file:///data/.../...png"

  let canvasImage = new CanvasImage(canvasRef.current)
  await new Promise(async resolve => {
    canvasImage.addEventListener('load', resolve)
    const base64 = await FileSystem.readAsStringAsync(path, { encoding: FileSystem.EncodingType.Base64 })
    canvasImage.src = 'data:image/png;base64,' + base64
  })

  ctx.drawImage(canvasImage, 0, 0, 100, 100)
1reaction
denislepagecommented, Dec 3, 2020

Unfortunately, not yet. I have put that project aside, but I am hoping to get back to it soon. Any suggestions would be appreciated.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Caching Bitmaps - Android Developers
A memory cache is useful in speeding up access to recently viewed bitmaps, however you cannot rely on images being available in this...
Read more >
Draw in apps with Markup on iPhone - Apple Support
Draw in apps with Markup on iPhone. In supported apps such as Mail, Messages, Notes, and Books, you can annotate photos, screenshots, PDFs,...
Read more >
The Sketch Data tool: Everything you need to know
With the Data tool dataset , you can add different images and text to your designs to create realistic mockups and prototypes.
Read more >
Here's the easiest way to delete application cache on your Mac
Application cache taking up too much space? Here's how to find temporary files, or caches, on your Mac so you can free up...
Read more >
android - Deletion of drawing cache - Stack Overflow
In my app I want to swap images at runtime when user clicks on it. there are two imageviews when user click on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found