首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Android中使用文件路径共享文件/从文件路径中获取工作uri

问在Android中使用文件路径共享文件/从文件路径中获取工作uri
EN

Stack Overflow用户
提问于 2021-01-04 09:04:40
回答 1查看 92关注 0票数 0

我正在尝试使用以下工具与其他应用程序(例如Telegram、Whatapp等)共享文件:

代码语言:javascript
复制
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
sendIntent.setType("text/plain");
startActivity(sendIntent);

如果uri来自ACTION_GET_CONTENT活动,它就会起作用。但是我只有一个要共享的文件的路径,如果我设置了:

代码语言:javascript
复制
uri = Uri.fromFile(new File(path))

看起来一切正常,但是在最后一步没有发送文件。

如何从文件路径中获取有效的uri?

EN

回答 1

Stack Overflow用户

发布于 2021-02-17 16:10:38

希望这段代码能对你有所帮助!

代码语言:javascript
复制
 ApplicationInfo api = getApplicationContext().getApplicationInfo();
                    String apkPath = api.sourceDir;
                    Intent share = new Intent(Intent.ACTION_SEND);
                    share.setType("application/vnd.android.package-archive");
                    share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(apkPath)));
                    startActivity(Intent.createChooser(share, "Share Via"));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65556548

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档