Skip to content

No reliable way to determine selected media's type #318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
nikoTM opened this issue Jan 27, 2020 · 5 comments
Open

No reliable way to determine selected media's type #318

nikoTM opened this issue Jan 27, 2020 · 5 comments

Comments

@nikoTM
Copy link

nikoTM commented Jan 27, 2020

Both videos and images are returned as ImageAsset, how can we reliably determine mediaType from there?

@pandabuilt
Copy link

Any idea anyone?

@nikoTM
Copy link
Author

nikoTM commented Apr 4, 2021

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]

@pandabuilt
Copy link

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

@nikoTM
Copy link
Author

nikoTM commented Apr 6, 2021

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

@davecoffin
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants