Skip to content
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

Bug: ERROR Class not found On Android (Capacitor 3) #36

Closed
AdlerJS opened this issue Sep 20, 2021 · 3 comments
Closed

Bug: ERROR Class not found On Android (Capacitor 3) #36

AdlerJS opened this issue Sep 20, 2021 · 3 comments

Comments

@AdlerJS
Copy link

AdlerJS commented Sep 20, 2021

When using on Capacitor 3 for Android I am getting the following error in the logcat console. Line 34005 - Msg: ERROR Class not found

I am using React and Capacitor 3 to implement the plugin which is working as expected on IOS but throws the error above on Android. Below is the custom hook that wraps this plugin.

const useNativeFilePreview = ({ fileId, isVault, workspaceId }) => {
  const [isLoading, setIsLoading] = useState(false);
  const isNative = Capacitor.isNativePlatform();
  const queryClient = useQueryClient();
  const toast = useToast();

  const fileContentArgs = isVault
    ? { queryClient, fileId }
    : { queryClient, fileId, workspaceId };

  const onSuccess = () => {
    setIsLoading(false);
  };

  const onError = (e) => {
    console.log('ERROR', e);
    toast({
      title: 'Error Opening File Note',
      description: 'Any error occured during preview.',
      status: 'error',
      duration: TOAST_DURATION,
      isClosable: true,
    });
    setIsLoading(false);
  };

  const previewNativeFile = async (fileName) => {
    if (isNative && !CACHE[fileName]) {
      setIsLoading(true);
      const fileContent = await downloadFileContents(fileContentArgs);
      const response = await fetch(fileContent);
      const blob = await response.blob();
      const result = await write_blob({
        path: fileName,
        blob: blob,
        directory: Directory.Cache,
      });
      console.log('FILE DL -- WRITE', result);
      CACHE[fileName] = result;
      await window.PreviewAnyFile.previewPath(onSuccess, onError, result);
    } else if (isNative && CACHE[fileName]) {
      await window.PreviewAnyFile.previewPath(
        onSuccess,
        onError,
        CACHE[fileName]
      );
    }
  };

  return { previewNativeFile, isLoading };
};

export default useNativeFilePreview;

I have been unable to trace down where exactly the error is occurring or what is causing it. However I can confirm it happens on the await window.PreviewAnyFile.previewPath(onSuccess, onError, result); line

@AdlerJS
Copy link
Author

AdlerJS commented Sep 20, 2021

I was able to solve it right after adding this issue but going to leave the solution below.

Step 1:
Make sure to add the required dependencies. via NPM install

    "cordova-plugin-androidx": "^3.0.0",
    "cordova-plugin-androidx-adapter": "^1.1.3",

Step 2:
If using capacitor since the Ionic Native package has not been updated you need to add the following to your android config.xml

    <feature name="PreviewAnyFile">
        <param name="android-package" value="com.mostafa.previewanyfile.PreviewAnyFile"/>
    </feature>

@AdlerJS
Copy link
Author

AdlerJS commented Sep 20, 2021

Closing this issue has the solution above should solve it for most users. Hopefully once Ionic Native update gets released the Step 2 will not be required. Thanks for the plugin!

@AdlerJS AdlerJS closed this as completed Sep 20, 2021
@mostafa-mansour1
Copy link
Owner

mostafa-mansour1 commented Sep 20, 2021 via email

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

2 participants