【问题标题】:Share Privately Stored Image to External App - Android将私人存储的图像共享到外部应用程序 - Android
【发布时间】:2014-06-28 16:58:45
【问题描述】:

我正在尝试将图像保存到我的应用程序的私有文件系统,然后与外部应用程序共享该图像。我已经调查过了,似乎最好的方法就是保存到filesDir,因为如果您使用externalFiles 目录,某些设备可能没有。到目前为止,这是我的代码(为简洁起见):

我的图片活动

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>

一旦发送意图,Twitter 应用程序就会崩溃。我查看了 Twitter 中的错误日志,似乎它可能是权限问题。我认为即使使用我目前拥有的代码,Twitter 也无权访问 Uri。

感谢所有帮助!

【问题讨论】:

  • 我建议发布您看到的实际堆栈跟踪或其他错误信息,以防您误解了您所看到的内容。
  • 感谢@CommonsWare 的建议。在仔细研究之后,我发现其他人也有类似的问题。解决了这个问题。

标签: android android-sharing android-fileprovider


【解决方案1】:

它不起作用的原因是许多 3rd-party 应用程序现在不支持 FileProvider。看到这个问题:Image share intent works for Gmail but crashes FB and twitter。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    相关资源
    最近更新 更多