use*_*234 38 java android sharing android-intent
我的Android应用程序中有一个文件列表,我希望能够获取所选项目并通过电子邮件或任何其他共享应用程序发送.这是我的代码.
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_EMAIL, getListView().getCheckedItemIds());
sendIntent.setType("text/plain");
startActivity(sendIntent);
Run Code Online (Sandbox Code Playgroud)
小智 48
这是在android中共享文件的代码
Intent intentShareFile = new Intent(Intent.ACTION_SEND);
File fileWithinMyDir = new File(myFilePath);
if(fileWithinMyDir.exists()) {
intentShareFile.setType("application/pdf");
intentShareFile.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+myFilePath));
intentShareFile.putExtra(Intent.EXTRA_SUBJECT,
"Sharing File...");
intentShareFile.putExtra(Intent.EXTRA_TEXT, "Sharing File...");
startActivity(Intent.createChooser(intentShareFile, "Share File"));
}
Run Code Online (Sandbox Code Playgroud)
Dig*_*tel 23
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(exportPath));
Run Code Online (Sandbox Code Playgroud)
你也可以制作zip file所有文件并附上zip文件,以便在android中发送多个文件
Shy*_*dda 11
对于那些在 Kotlin 中尝试的人来说,方法如下:
像下面这样开始意图:
fun startFileShareIntent(filePath: String) { // pass the file path where the actual file is located.
val shareIntent = Intent(Intent.ACTION_SEND).apply {
type = FILE_TYPE // "*/*" will accepts all types of files, if you want specific then change it on your need.
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
flags = Intent.FLAG_ACTIVITY_NEW_TASK
putExtra(
Intent.EXTRA_SUBJECT,
"Sharing file from the AppName"
)
putExtra(
Intent.EXTRA_TEXT,
"Sharing file from the AppName with some description"
)
val fileURI = FileProvider.getUriForFile(
context!!, context!!.packageName + ".provider",
File(filePath)
)
putExtra(Intent.EXTRA_STREAM, fileURI)
}
startActivity(shareIntent)
}
Run Code Online (Sandbox Code Playgroud)
在应用程序标签内的清单中:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
Run Code Online (Sandbox Code Playgroud)
res-->xml--> provider_paths.xml下
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path name="files" path="." />
<external-path name="external_files" path="."/>
</paths>
Run Code Online (Sandbox Code Playgroud)
这适用于每个文件!
private void shareFile(File file) {
Intent intentShareFile = new Intent(Intent.ACTION_SEND);
intentShareFile.setType(URLConnection.guessContentTypeFromName(file.getName()));
intentShareFile.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file://"+file.getAbsolutePath()));
//if you need
//intentShareFile.putExtra(Intent.EXTRA_SUBJECT,"Sharing File Subject);
//intentShareFile.putExtra(Intent.EXTRA_TEXT, "Sharing File Description");
startActivity(Intent.createChooser(intentShareFile, "Share File"));
}
Run Code Online (Sandbox Code Playgroud)
谢谢Tushar-Mate!
| 归档时间: |
|
| 查看次数: |
40459 次 |
| 最近记录: |