简体   繁体   中英

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();
}

If it's not already did :

Try to go to your settings->Application->YourApp; then click permission and authorize access to storage.

But you should ask the permission directly from the app. The exemple of the official doc is simple : https://developer.android.com/training/permissions/requesting.html

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM