2

I'm trying to save an image to my app's private filesystem then share that image with external apps. I've looked into it, and it seems like the best way to do it is just to save onto the filesDir because if you use the externalFiles directory, some devices may not have one. Here's my code so far (simplified for brevity):

Activity with my image

public Uri saveImageAndGetUri(Context context) {
    String fileName = "test.png";
    File imageDirectory = new File(getFilesDir(), "images");
    File savedImage = new File(imageDirectory, fileName);
    FileOutputStream stream;
    try {
        stream = new FileOutputStream(savedImage);
        getCurrentImage().compress(Bitmap.CompressFormat.PNG, 100, stream);
        stream.flush();
        stream.close();
    } catch(Exception e) {}
    return FileProvider.getUriForFile(context, "com.mydomain.mypackage.Dialogs.ShareOptions", savedImage);
}

ShareOptions.java

Uri contentUri = activity.saveImageAndGetUri(getContext()); //This calls the method above
intent = new Intent(Intent.ACTION_SEND);
getContext().grantUriPermission("com.twitter.android", contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION); //Grant read permission to Twitter. Don't think this part is working
getContext().grantUriPermission("com.twitter.android", contentUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION); //Grant write permission to Twitter. Don't think this part is working
intent.setPackage("com.twitter.android");
intent.setType("image/png");
intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(Intent.EXTRA_STREAM, contentUri);

AndroidManifest.xml

<provider android:name="android.support.v4.content.FileProvider"
    android:authorities="com.mydomain.mypackage.Dialogs.ShareOptions"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths"/>
</provider>

xml/file_paths.xml

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="my_images" path="images/"/>
</paths>

As soon as the intent is sent, the Twitter app crashes. I've looked into the error log in Twitter and it seems like it may be an issue with permissions. I think somehow Twitter doesn't have permission to access the Uri even with the code I currently have.

Any and all help is appreciated!

MrGrinst
  • 970
  • 3
  • 9
  • 20
  • I would suggest posting the actual stack trace or other error information you are seeing, in case you are misinterpreting what you are seeing. – CommonsWare Jun 28 '14 at 17:02
  • Thanks for the advice @CommonsWare. After looking into it more I found that someone else had a similar issue. Resolved the question. – MrGrinst Jun 28 '14 at 17:10

1 Answers1

1

The reason it doesn't work is many 3rd-party apps don't support FileProvider right now. See this question: Image share intent works for Gmail but crashes FB and twitter.

Community
  • 1
  • 1
MrGrinst
  • 970
  • 3
  • 9
  • 20