Coder Social home page Coder Social logo

Comments (5)

pandabuilt avatar pandabuilt commented on June 11, 2024

Any idea anyone?

from nativescript-imagepicker.

nikoTM avatar nikoTM commented on June 11, 2024
private resolveMediaType(path: string): string | CustomError {
    if (!path) {
      return new CustomError(
        this.translateService.instant('services.noFileProvided')
      );
    }
    try {
      if (isAndroid) {
        const cr = Application.android.context.getContentResolver();
        const file = new java.io.File(path);
        const sdkVersionInt = parseInt(Device.sdkVersion, 10);
        const pkgName = Utils.ad.getApplication().getPackageName();
        let mediaUri: string;
        if (sdkVersionInt >= 21) {
          const ContentNamespace =
            global.androidx && global.androidx.appcompat
              ? androidx.core.content
              : android.support.v4.content;
          mediaUri = ContentNamespace.FileProvider.getUriForFile(
            Application.android.foregroundActivity, // or app.android.currentContext ??
            `${pkgName}.provider`,
            file
          );
        } else {
          mediaUri = android.net.Uri.fromFile(file);
        }

        return cr.getType(mediaUri);
      } else if (isIOS) {
        const mediaType = (path as any).mediaType;

        switch (mediaType) {
          case PHAssetMediaType.Image:
            return 'image/*';
          case PHAssetMediaType.Video:
            return 'video/*';
        }

        return null;
      }
    } catch (e) {
      console.log(e);

      if (typeof path !== 'string') {
        return new CustomError(
          this.translateService.instant('services.unableToResolveMediaType')
        );
      }
      const possibleImageFormats = ['.jpg', '.jpeg', '.png'];
      return possibleImageFormats.some(format => path.endsWith(format))
        ? 'image/*'
        : new CustomError(
            this.translateService.instant('services.unableToResolveMediaType')
          );
    }
  }
  

@pandabuilt I use this as a workaround, path being ImageAsset.[android | ios]

from nativescript-imagepicker.

pandabuilt avatar pandabuilt commented on June 11, 2024

@nikoTM Can I see your imports? Or how do you get global.androidx, java and PHAssetMediaType

from nativescript-imagepicker.

nikoTM avatar nikoTM commented on June 11, 2024

@pandabuilt declare const global: any, java: any, PHAssetMediaType: any; those are available at runtime

from nativescript-imagepicker.

davecoffin avatar davecoffin commented on June 11, 2024

in 2.0.0, it will now return selection[0].type and be image|video.

from nativescript-imagepicker.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.