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.

[@nativescript/imagepicker] Returns `ImageAsset` with `content://` scheme that can't be loaded with `ImageSource.fromAsset`

See original GitHub issue

Versions

@nativescript/core: 7.3.0
@nativescript/android: 7.0.1
@nativescript/imagepicker: 1.0.5

Problem

Using @nativescript/imagepicker in Android 10, the returned selected image has the following form when logged to console:

{
  "_observers": {},
   "_options": {
     "keepAspectRatio": true,
     "autoScaleFactor": true
 },
"_android": "content://com.android.providers.media.documents/document/image%3A29"

When trying to load this ImageAsset into an ImageSource with ImageSource.fromAsset I get an Asset 'content://com.android.providers.media.documents/document/image%3A29' cannot be found. error.

Example code

let context = imagepicker.create({
      mode: "single",
      mediaType: ImagePickerMediaType.Image,
    });

    context
      .authorize()
      .then(() => {
        return context.present();
      })
      .then((selection) => {        
        if (selection.length > 0) {
          ImageSource.fromAsset(selection[0])
            .then((imageSource) => {
              console.log("ImageSource loaded:", imageSource);
            })
            .catch((err) => console.error("Error loading ImageSource:", err));
        }
      });

Is this a bug, or is there another way to get an ImageSource from the result of the imagepicker selection?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
jamescodesthingscommented, Jul 16, 2021

As a heads up the issue looks to be this line: https://github.com/NativeScript/plugins/blob/master/packages/imagepicker/index.android.ts#L198

I’m getting these Uris returned on API Level 30 devices which suggests this line is causing the problem. Manually setting this to true fixed the issue for API Level 30 devices but I can’t be sure of the impact.

To workaround the problem in my project I’ve:

  1. copied UriHelper into my own code as an exported class.
  2. Wrapped my old ImageSource.fromAsset in a try catch
  3. In the catch, if android, call UriHelper._calculateFileUri and try again.

Looks something like this:

      await picker.authorize();
      const result = await picker.present();

      // No images selected then return null
      // Should return here
      if (!result?.length) return null;

      const path = ImagePaths.rawImagePath();
      const asset: ImageAsset = result[0];
      try {
        const image = await ImageSource.fromAsset(asset);
        image.saveToFile(path, IMAGE_FORMAT);
        return path;
      } catch (error) {
        // This the new bit while waiting on #145
        if (isAndroid) {
          const url = asset?.android;
          if (!url) throw error;
          const uri = android.net.Uri.parse(url);

          const rawUrl = UriHelper._calculateFileUri(uri);
          if (!rawUrl) throw error;

          const newAsset = new ImageAsset(rawUrl);
          const image = await ImageSource.fromAsset(newAsset);
          image.saveToFile(path, IMAGE_FORMAT);
          return path;
        }
        throw error;
      }
0reactions
sosclercommented, Nov 14, 2021

Same here. I am using @jamescodesthings hack while waiting for a fix

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nativescript imageSource.fromAsset getImageAsync error
I'm using nativescript-imagepicker for the selection of the image : let context = imagePicker.create({ mode : "single" }); context.authorize() .
Read more >
ImageSource.fromAsset() memory leak on iOS #9694 - GitHub
Issue Description On iOS my app is crashing because it eats all the memory. I used Xcode profiler to find out what's causing...
Read more >
Class ImageSource - NativeScript Docs
Loads this instance from the specified base64 encoded string asynchronously. Parameters. source: string. The Base64 string to load the image from. Returns ......
Read more >
Nativescript imagepicker not working in iOS - Anycodings.com
I am now saving the image to a temporary anycodings_nativescript location and it is still not working in iOS. anycodings_nativescript It works ...
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