moberwasserlechner / capacitor-filesharer

Capacitor plugin to download and share files for the Web, Android and iOS! Stop the war in Ukraine!
MIT License
82 stars 20 forks source link

Bug: App crashes upon sharing file on whatsapp #26

Closed arbaaz360 closed 1 year ago

arbaaz360 commented 3 years ago

Capacitor version:

Run npx cap doctor:

  @capacitor/cli 2.4.2
  @capacitor/core 2.4.2
  @capacitor/android 2.4.2

Library version:

Your Plugin Configuration

{
  "name": "dilemma",
  "version": "0.0.1",
  "author": "Ionic Framework",
  "homepage": "https://ionicframework.com/",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "~10.0.0",
    "@angular/core": "~10.0.0",
    "@angular/fire": "^6.0.3",
    "@angular/forms": "~10.0.0",
    "@angular/platform-browser": "~10.0.0",
    "@angular/platform-browser-dynamic": "~10.0.0",
    "@angular/router": "~10.0.0",
    "@byteowls/capacitor-filesharer": "^2.0.0",
    "@capacitor/android": "^2.4.2",
    "@capacitor/core": "2.4.2",
    "@codetrix-studio/capacitor-google-auth": "^2.1.1",
    "@ionic-native/core": "^5.0.0",
    "@ionic-native/google-plus": "^5.28.0",
    "@ionic-native/social-sharing": "^5.28.0",
    "@ionic-native/splash-screen": "^5.0.0",
    "@ionic-native/status-bar": "^5.0.0",
    "@ionic/angular": "^5.0.0",
    "angularfire2": "^5.4.2",
    "axios": "^0.20.0",
    "capacitor-firebase-auth": "^2.3.2",
    "cordova-plugin-x-socialsharing": "^6.0.2",
    "es6-promise-plugin": "^4.2.2",
    "firebase": "^7.22.1",
    "rxjs": "~6.5.5",
    "tslib": "^2.0.0",
    "zone.js": "~0.10.3"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.1000.0",
    "@angular/cli": "~10.0.5",
    "@angular/compiler": "~10.0.0",
    "@angular/compiler-cli": "~10.0.0",
    "@angular/language-service": "~10.0.0",
    "@capacitor/cli": "2.4.2",
    "@ionic/angular-toolkit": "^2.3.0",
    "@types/jasmine": "~3.5.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^12.11.1",
    "codelyzer": "^6.0.0",
    "jasmine-core": "~3.5.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~5.0.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~3.0.2",
    "karma-jasmine": "~3.3.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "protractor": "~7.0.0",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~3.9.5"
  },
  "description": "An Ionic project",
  "browser": {
    "fs": false,
    "path": false,
    "os": false
  }
}

Affected Platform(s):

Current Behavior

I am able to share files on WhatsApp without any problems except that after sharing the file if you press back, you don't get back to the app. The app has to be restarted.

Expected Behavior

Upon successful sharing of file on whatApp and then clicking on android back button , user should get back to the app. The app shouldnt crash and should be have to be started again.

Sample Code or Sample Application Repo

      Plugins.FileSharer.share({
          filename: "test2.jpg",
          text:"www.xyz.com",
          base64Data:  imageBase64 ,
          contentType: "image/jpeg",
      }).then(() => {
          // empty
      }).catch(error => {
          console.error("File sharing failed", error.message);
      });

Reproduction Steps

Just share a picture via whatsapp and then get back to the app. You would find that it gets restarted.

Other Information

The following error gets logged in android studio console as soon as you click share:

E/DatabaseUtils: Writing exception to parcel
    java.lang.SecurityException: Permission Denial: reading com.byteowls.capacitor.filesharer.FileSharerProvider uri content://io.ionic.starter.filesharer.fileprovider/capfilesharer/test2.jpg from pid=14504, uid=1000 requires the provider be exported, or grantUriPermission()
        at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:820)
        at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:684)
        at android.content.ContentProvider$Transport.enforceFilePermission(ContentProvider.java:674)
        at android.content.ContentProvider$Transport.openTypedAssetFile(ContentProvider.java:548)
        at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:327)
        at android.os.Binder.execTransactInternal(Binder.java:1154)
        at android.os.Binder.execTransact(Binder.java:1123)
KrishPro commented 3 years ago

facing same issue with all platforms. Any Updates ?

dtrigo commented 3 years ago

Hi, I-m affected by this issue too.

I read about this exception in other libraries too, so waiting for someone to solve it...

KrishPro commented 3 years ago

You can check if you had made any mistake in configuring You can check from README.md of this repo

For me it solved by adding,

@Override
public void onSaveInstanceState(Bundle outState) {
   super.onSaveInstanceState(outState);
   outState.clear();
}

to MainActivity.java just after OnCreate() function. Like This :-

public class MainActivity extends BridgeActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Initializes the Bridge
    this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
      // Additional plugins you've installed go here
      // Ex: add(TotallyAwesomePlugin.class);
      add(FileSharerPlugin.class);
    }});
  }
  @Override
  public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.clear();
  }
}

Leave Comment, if it solves or not

dtrigo commented 3 years ago

Thank you for answering, but I-m afraid it didn't.

I already had that override on my MainActivity.java, the issue is not happening on a physical device running Android < 10

I also tried to add <application android:requestLegacyExternalStorage="true" ...> to my Manifest as I saw somewhere (apparently related to this issue), obviously no luck either

moberwasserlechner commented 2 years ago

I released the new version 3.0.0 today.

If you can and it is still relevant please test this issue with the new version. (Capacitor 3.0.0 required)

I keep this issue open a few days and close it afterwards if there is no feedback.

BR Michael

wcooperscott commented 2 years ago

Howdy @moberwasserlechner, I too am facing this issue when sharing to Instagram. Instagram is opened successfully with the correct data, however, upon returning the app, it crashes.

mhamri commented 2 years ago

@moberwasserlechner I am facing the same issue:

2022-05-01 02:03:11.328 2157-2450/com.company.appName E/DatabaseUtils: Writing exception to parcel
    java.lang.SecurityException: Permission Denial: reading com.byteowls.capacitor.filesharer.FileSharerProvider uri content://com.company.appName.filesharer.fileprovider/capfilesharer/appName-Sunday-2022%2005%201_02%203%2009_GMT8.jpg from pid=31221, uid=1000 requires the provider be exported, or grantUriPermission()
        at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:912)
        at android.content.ContentProvider.semEnforceReadPermission(ContentProvider.java:830)
        at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:708)
        at android.content.ContentProvider$Transport.query(ContentProvider.java:247)
        at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:107)
        at android.os.Binder.execTransactInternal(Binder.java:1215)
        at android.os.Binder.execTransact(Binder.java:1179)
2022-05-01 02:03:11.331 2157-2450/com.company.appName E/DatabaseUtils: Writing exception to parcel
    java.lang.SecurityException: Permission Denial: reading com.byteowls.capacitor.filesharer.FileSharerProvider uri content://com.company.appName.filesharer.fileprovider/capfilesharer/appName-Sunday-2022%2005%201_02%203%2009_GMT8.jpg from pid=31221, uid=1000 requires the provider be exported, or grantUriPermission()
        at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:912)
        at android.content.ContentProvider.semEnforceReadPermission(ContentProvider.java:830)
        at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:708)
        at android.content.ContentProvider$Transport.enforceFilePermission(ContentProvider.java:698)
        at android.content.ContentProvider$Transport.openTypedAssetFile(ContentProvider.java:557)
        at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:327)
        at android.os.Binder.execTransactInternal(Binder.java:1215)
        at android.os.Binder.execTransact(Binder.java:1179)

this is my android manifest

<?xml version='1.0' encoding='utf-8'?>
<manifest package="com.company.appName" xmlns:android="http://schemas.android.com/apk/res/android">
    <application 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" android:usesCleartextTraffic="true">
        <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="@string/admob_app_id" />
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode" android:label="@string/title_activity_main" android:launchMode="singleTask" android:name="com.company.appName.MainActivity" android:theme="@style/AppTheme.NoActionBarLaunch">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <provider android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true" android:name="androidx.core.content.FileProvider">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" />
        </provider>
    </application>
    <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" />
</manifest>

and this is file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="." />
    <cache-path name="my_cache_images" path="." />
</paths>

looking at code I couldn't figure out the issue, maybe need to change androidx.core.content.FileProvider package to android.support.v4.content.FileProvider https://developer.android.com/reference/android/support/v4/content/FileProvider#ProviderDefinition

i'm using "@byteowls/capacitor-filesharer": "^3.0.0",

and the device i'm testing on is android 12.0 API Level 31

KrishPro commented 2 years ago

@moberwasserlechner I am facing the same issue:

2022-05-01 02:03:11.328 2157-2450/com.company.appName E/DatabaseUtils: Writing exception to parcel
    java.lang.SecurityException: Permission Denial: reading com.byteowls.capacitor.filesharer.FileSharerProvider uri content://com.company.appName.filesharer.fileprovider/capfilesharer/appName-Sunday-2022%2005%201_02%203%2009_GMT8.jpg from pid=31221, uid=1000 requires the provider be exported, or grantUriPermission()
        at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:912)
        at android.content.ContentProvider.semEnforceReadPermission(ContentProvider.java:830)
        at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:708)
        at android.content.ContentProvider$Transport.query(ContentProvider.java:247)
        at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:107)
        at android.os.Binder.execTransactInternal(Binder.java:1215)
        at android.os.Binder.execTransact(Binder.java:1179)
2022-05-01 02:03:11.331 2157-2450/com.company.appName E/DatabaseUtils: Writing exception to parcel
    java.lang.SecurityException: Permission Denial: reading com.byteowls.capacitor.filesharer.FileSharerProvider uri content://com.company.appName.filesharer.fileprovider/capfilesharer/appName-Sunday-2022%2005%201_02%203%2009_GMT8.jpg from pid=31221, uid=1000 requires the provider be exported, or grantUriPermission()
        at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:912)
        at android.content.ContentProvider.semEnforceReadPermission(ContentProvider.java:830)
        at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:708)
        at android.content.ContentProvider$Transport.enforceFilePermission(ContentProvider.java:698)
        at android.content.ContentProvider$Transport.openTypedAssetFile(ContentProvider.java:557)
        at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:327)
        at android.os.Binder.execTransactInternal(Binder.java:1215)
        at android.os.Binder.execTransact(Binder.java:1179)

this is my android manifest

<?xml version='1.0' encoding='utf-8'?>
<manifest package="com.company.appName" xmlns:android="http://schemas.android.com/apk/res/android">
    <application 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" android:usesCleartextTraffic="true">
        <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="@string/admob_app_id" />
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode" android:label="@string/title_activity_main" android:launchMode="singleTask" android:name="com.company.appName.MainActivity" android:theme="@style/AppTheme.NoActionBarLaunch">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <provider android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true" android:name="androidx.core.content.FileProvider">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" />
        </provider>
    </application>
    <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" />
</manifest>

and this is file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="." />
    <cache-path name="my_cache_images" path="." />
</paths>

looking at code I couldn't figure out the issue, maybe need to change androidx.core.content.FileProvider package to android.support.v4.content.FileProvider https://developer.android.com/reference/android/support/v4/content/FileProvider#ProviderDefinition

i'm using "@byteowls/capacitor-filesharer": "^3.0.0",

and the device i'm testing on is android 12.0 API Level 31

Umm, You can try making a separate poc. Just featuring file sharing and Make Sure you do the setup carefully. I am saying this because i believe there is nothing wrong with the plugin.

Earlier, I also had issues with this plugin but after fixing a few errors i made while setting it up, I worked.

mhamri commented 2 years ago

@KrishPro thanks, but what kind of bug did you have? it was in angular or android side? bcs both are building just fine and it happens only if I click on the sharing, even the try catch around the capacitor connection doesn't throw any exception and the only way to capture the error message is through logcat. since I posted almost every setting that I have in my application, could you point out where to look at? maybe I can give it a try and reinstall the package again, but looking at the setting, it's quite basic standard that doesn't look anything wrong with them

mhamri commented 2 years ago

You can check if you had made any mistake in configuring You can check from README.md of this repo

For me it solved by adding,

@Override
public void onSaveInstanceState(Bundle outState) {
   super.onSaveInstanceState(outState);
   outState.clear();
}

to MainActivity.java just after OnCreate() function. Like This :-

public class MainActivity extends BridgeActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Initializes the Bridge
    this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
      // Additional plugins you've installed go here
      // Ex: add(TotallyAwesomePlugin.class);
      add(FileSharerPlugin.class);
    }});
  }
  @Override
  public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.clear();
  }
}

Leave Comment, if it solves or not

@KrishPro looking at this, it's not part of the ReadMe.md (anymore) and capacitor installation should take care of the installation, do I still need to add these stuffs?

KrishPro commented 2 years ago

See #25 And tell if my comments there solve the issue for you.

moberwasserlechner commented 1 year ago

1) https://github.com/moberwasserlechner/capacitor-filesharer#platform-android 2) In my app's AndroidManifest I have a custom FileProvider configured

    <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" />
    </provider>

3) Current tests show that these permission should not be needed by the plugin but I'm only started to bring my app to Capacitor 4 (I know there is Cap5 but it's to much effort to do it right now).

  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

4) Relates to #42 and #32. It seems to me that this issue mixes problems