I am trying to use file storage to read in files from the camera or photo library. Upon applying the selected camera photo or the imported photo library photo, the app crashes and gives the error below. Also below is the source code I have.
Guide I used for file service implementation:
https://devdactic.com/ionic-image-upload-capacitor/
Other people with related issues:
[
Capacitor Filesystem error when I try to read a file in Android 11
](
https://Ionic
Forum)
[
typescript - Capacitor Filesystem error when I try to read a file in Android 11 - Stack Overflow
](
https://Stack
Overflow)
File Service:
async saveImageMobile(imageData: any, containerType: string) {
const base64Data = await this.readAsBase64(imageData).toString();
console.log(base64Data);
const fileName = new Date().getTime() + '.jpeg';
const savedFile = await Filesystem.writeFile({
path: `${IMAGE_DIR}/${fileName}`,
data: base64Data,
directory: Directory.Data
readAsBase64(photo: Photo) {
return new Promise(async (resolve, reject) => {
const file = await Filesystem.readFile({
path: photo.path
resolve(true);
return file.data;
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<application
android:requestLegacyExternalStorage="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode"
android:name="com.myapp.app.MainActivity"
android:label="@string/title_activity_main"
android:theme="@style/AppTheme.NoActionBarLaunch"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
</application>
<!-- Permissions -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Android Studio Error:
V/Capacitor/Plugin: To native (Cordova plugin): callbackId: Camera947168117, service: Camera, action: takePicture, actionArgs: [30,0,0,-1,-1,0,0,false,false,false,null,0]
D/Capacitor: App paused
D/Capacitor: App stopped
D/Capacitor: Saving instance state!
D/Capacitor: Unable to find a Capacitor plugin to handle requestCode, trying Cordova plugins 17
D/Capacitor: App restarted
D/Capacitor: App started
D/Capacitor: App resumed
V/Capacitor/Plugin: To native (Capacitor plugin): callbackId: 52554790, pluginId: Filesystem, methodName: readFile
V/Capacitor: callback: 52554790, pluginId: Filesystem, methodName: readFile, methodData: {}
E/Capacitor: Serious error executing plugin
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:121)
at com.getcapacitor.Bridge.lambda$callPluginMethod$0$Bridge(Bridge.java:592)
at com.getcapacitor.-$$Lambda$Bridge$25SFHybyAQk7zS27hTVXh2p8tmw.run(Unknown Source:8)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.os.HandlerThread.run(HandlerThread.java:67)
Caused by: java.lang.NullPointerException: uriString
at android.net.Uri$StringUri.(Uri.java:496)
at android.net.Uri$StringUri.(Uri.java:486)
at android.net.Uri.parse(Uri.java:458)
at com.capacitorjs.plugins.filesystem.Filesystem.getInputStream(Filesystem.java:138)
at com.capacitorjs.plugins.filesystem.Filesystem.readFile(Filesystem.java:24)
at com.capacitorjs.plugins.filesystem.FilesystemPlugin.readFile(FilesystemPlugin.java:62)
at java.lang.reflect.Method.invoke(Native Method)
at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:121)
at com.getcapacitor.Bridge.lambda$callPluginMethod$0$Bridge(Bridge.java:592)
at com.getcapacitor.-$$Lambda$Bridge$25SFHybyAQk7zS27hTVXh2p8tmw.run(Unknown Source:8)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.os.HandlerThread.run(HandlerThread.java:67)
I/Capacitor/Console: File: http://localhost/main-es2015.js - Line 1 - Msg: [object Promise]
E/AndroidRuntime: FATAL EXCEPTION: CapacitorPlugins
Process: com.myapp.app, PID: 11812
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.getcapacitor.Bridge.lambda$callPluginMethod$0$Bridge(Bridge.java:601)
at com.getcapacitor.-$$Lambda$Bridge$25SFHybyAQk7zS27hTVXh2p8tmw.run(Unknown Source:8)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.os.HandlerThread.run(HandlerThread.java:67)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:121)
at com.getcapacitor.Bridge.lambda$callPluginMethod$0$Bridge(Bridge.java:592)
at com.getcapacitor.-$$Lambda$Bridge$25SFHybyAQk7zS27hTVXh2p8tmw.run(Unknown Source:8)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.os.HandlerThread.run(HandlerThread.java:67)
Caused by: java.lang.NullPointerException: uriString
at android.net.Uri$StringUri.(Uri.java:496)
at android.net.Uri$StringUri.(Uri.java:486)
at android.net.Uri.parse(Uri.java:458)
at com.capacitorjs.plugins.filesystem.Filesystem.getInputStream(Filesystem.java:138)
at com.capacitorjs.plugins.filesystem.Filesystem.readFile(Filesystem.java:24)
at com.capacitorjs.plugins.filesystem.FilesystemPlugin.readFile(FilesystemPlugin.java:62)
at java.lang.reflect.Method.invoke(Native Method)
at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:121)
at com.getcapacitor.Bridge.lambda$callPluginMethod$0$Bridge(Bridge.java:592)
at com.getcapacitor.-$$Lambda$Bridge$25SFHybyAQk7zS27hTVXh2p8tmw.run(Unknown Source:8)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.os.HandlerThread.run(HandlerThread.java:67)
V/Capacitor/Plugin: To native (Capacitor plugin): callbackId: 52554791, pluginId: Filesystem, methodName: writeFile
V/Capacitor: callback: 52554791, pluginId: Filesystem, methodName: writeFile, methodData: {“path”:“stored-images/1638597385177.jpeg”,“data”:"[object Promise]",“directory”:“DATA”}
I/Process: Sending signal. PID: 11812 SIG: 9
Reddit of same post incase someone ever comes across this later on and sees a resolution