2016-04-23 108 views
0

我无法共享我下载的文件。我的代码现在的问题是Android共享文件

Intent intent = new Intent(Intent.ACTION_SEND); 
intent.setDataAndType(uri, "text/html"); 
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
startActivity(intent); 

的文件是在外部缓存目录

选择器打开,但没有应用程序可以处理它。 ES浏览器给我“文件未找到”,其他人以类似的方式失败。我会用DownloadManager,但我通过POST得到这个文件。我还没有尝试过使用FileProvider或媒体商店,但这些看起来像是下一个选项。

我现在的主要需求是获取文件并将其提供给用户。什么是最简单的方法来做到这一点?

+0

什么是 “它给用户” 是什么意思?这不是你如何使用ACTION_SEND。如果您将其切换到“ACTION_VIEW”,并且摆脱了“addFlags()”,那么您将拥有有效的代码来查看该文件,至少在Android N发布之前。 – CommonsWare

+0

'ACTION_VIEW'的作品,但它只给我几个应用程序,允许查看。我需要将它发送到某处,例如Dropbox或电子邮件。 – DariusL

回答

1

战术,你的代码改成这样:

Intent intent = new Intent(Intent.ACTION_SEND); 
intent.setType("text/html"); 
intent.putExtra(Intent.EXTRA_STREAM, uri); 
startActivity(intent); 

以下the documentation for ACTION_SEND

从战略上看,由于sharing files is being discontinued,加a FileProvider to serve your file,如the sharing files documentation所示。

+0

我只是用这段代码写了一个答案。谢谢,我会研究这些变化。不是[MediaStore.Files](http://developer.android.com/reference/android/provider/MediaStore.Files.html)也是一个有效的解决方案,特别是如果你不想从用户隐藏你的文件? – DariusL

+0

@DariusL:“不是MediaStore.Files也是一个有效的解决方案......” - 我不是粉丝。我认为'MediaStore'是只读的。不同的人一直在使用它来读写,我祝他们好运。 – CommonsWare

1

更新你的代码与下面的代码: -

Intent shareIntent = new Intent(); 
shareIntent.setAction(Intent.ACTION_SEND); 
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(filePath))); 
shareIntent.setType("text/html"); 
startActivity(Intent.createChooser(shareIntent,"Share")); 
+0

对不起,另一个人是早5分钟 – DariusL

+0

@DariusL没问题.... –