Android共享文件,通过电子邮件或其他应用程序发送

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)

  • 除此之外,我需要设置一个“ FileProvider”,这可能会有所帮助:https://guides.codepath.com/android/Sharing-Content-with-Intents (2认同)
  • 我仍然无法共享文件。当我尝试将其附加到 gmail 中时,它向我显示了 toast 中的错误。请帮忙。谢谢 (2认同)

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)


Jav*_*ati 7

这适用于每个文件!

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!

  • 一些信息,您的代码将对发问者有所帮助。 (2认同)
  • 令人惊叹-充满魅力 (2认同)
  • 那FileProvider呢? (2认同)