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



我正在尝试使用与其他应用程序(如Telegram、Whatapp等(共享文件

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活动,则该方法有效。但我只有一个我想共享的文件的路径,如果我设置:

uri = Uri.fromFile(new File(path))

一切似乎都很好,但文件没有在最后一步发送。

如何从文件路径获取工作uri?

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

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"));