androidpermissionsshareappceleratorfilereference

Appcelerator: Share Screenshot of a specific view as a File cause Permission denied message


TargetSDK 23, Titanium SDK 5.4.0

Permissions set:

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

I take a Screenshot of a view by writing it to a file. That worked, because I can add this file to an imageview and see the image.

var blob = masterView.views[currentSavedPage].toImage();
    var file = Titanium.Filesystem.getFile(Titanium.Filesystem.externalStorageDirectory, "myNewImage.jpg");
    file.write(blob);

As I try to share the image with additional text, the other App (Facebook, Whatsapp, ...) can't access the image.

intent = Ti.Android.createIntent({
            action : Ti.Android.ACTION_SEND,
            type : "image/jpeg"
        });
        intent.putExtra(Ti.Android.EXTRA_TEXT, text);
        intent.putExtra(Ti.Android.EXTRA_SUBJECT, subject);
        intent.putExtraUri(Ti.Android.EXTRA_STREAM, file.nativePath);
        share = Ti.Android.createIntentChooser(intent, 'Bild teilen');

I only got a Permission denied as Error and don't know how to fix this. This worked with an lower SDK Version.

fb4a.RequestLoggingListener: java.io.FileNotFoundException: /data/user/0/de.myapp.id/app_appdata/myNewImage.jpg: open failed: EACCES (Permission denied)
ExifInterface: java.io.FileNotFoundException: /data/user/0/de.myapp.id/app_appdata/myNewImage.jpg: open failed: EACCES (Permission denied)
BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /data/user/0/de.myapp.id/app_appdata/myNewImage.jpg: open failed: EACCES (Permission denied)

Solution:

if (!Ti.Filesystem.hasStoragePermissions()) {
  Ti.Filesystem.requestStoragePermissions(function(result) {
    if (result.success) {
      openShareIntent();
    } else {
      alert('Permissions denied.');
    }
  });
} else {
  openShareIntent();
}

Solution

  • You need to implement runtime permissions for Android 6.0 and above. As it does not have the permission, even though defined in the manifest or tiapp.xml. Provide runtime permission and it should not give permission errors.