Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android共享文件,通过电子邮件或其他应用程序发送_Java_Android_Android Intent_Sharing - Fatal编程技术网

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

Java Android共享文件,通过电子邮件或其他应用程序发送,java,android,android-intent,sharing,Java,Android,Android Intent,Sharing,我的android应用程序中有一个文件列表,我希望能够获取所选项目并通过电子邮件或任何其他共享应用程序发送它们。这是我的密码 Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_EMAIL, getListView().getCheckedItemIds

我的android应用程序中有一个文件列表,我希望能够获取所选项目并通过电子邮件或任何其他共享应用程序发送它们。这是我的密码

Intent sendIntent = new Intent();
                    sendIntent.setAction(Intent.ACTION_SEND);
                    sendIntent.putExtra(Intent.EXTRA_EMAIL, getListView().getCheckedItemIds());
                    sendIntent.setType("text/plain");
                    startActivity(sendIntent);
此外,您还可以将所有文件的
zip文件
,并附加用于在android中发送多个文件的zip文件

用于向某人发送多个数据

intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, arrayUri);
intent.setType("text/plain");
startActivity(intent);
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"));
}
arrayUri
是要发送的文件的Uri数组列表。

阅读本文


这是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"));
}

以下是共享或保存文本文件的示例:

private void shareFile(String filePath) {

    File f = new File(filePath);

    Intent intentShareFile = new Intent(Intent.ACTION_SEND);
    File fileWithinMyDir = new File(filePath);

    if (fileWithinMyDir.exists()) {
        intentShareFile.setType("text/*");
        intentShareFile.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + filePath));
        intentShareFile.putExtra(Intent.EXTRA_SUBJECT, "MyApp File Share: " + f.getName());
        intentShareFile.putExtra(Intent.EXTRA_TEXT, "MyApp File Share: " + f.getName());

        this.startActivity(Intent.createChooser(intentShareFile, f.getName()));
    }
}

这是每一个文件的工作

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

}

谢谢你,朋友

首先应定义文件提供程序,请参阅

代码检查设备是否包含可接收文件的应用程序,请参阅

乐趣共享PDF(文件:文件,上下文:上下文){ val uri=getUriFromFile(文件,上下文) if(uri!=null){ val intent=intent().apply{ action=Intent.action\u发送 type=“application/pdf”//用于pdf文件。 putExtra(Intent.EXTRA_流,uri) putExtra(Intent.EXTRA_SUBJECT,file.name) putExtra(Intent.EXTRA_TEXT,file.name) //向内容URI授予临时读取权限。 addFlags(Intent.FLAG\u GRANT\u READ\u URI\u权限) } //验证设备是否可以打开您的文件。 val activityInfo=intent.resolveActivityInfo(context.packageManager,intent.flags) if(activityInfo?.exported==true){ context.startActivity(Intent.createChooser(Intent, “共享PDF文件”) } } } 有趣的getUriFromFile(文件:文件,上下文:上下文):Uri= if(Build.VERSION.SDK\u INT
val-UrArrayList:ArrayList=ArrayList()
GlobalScope.launch(Dispatchers.IO){
奔跑{
项目列表!!.forEach{
uriArrayList.add(
FileProvider.getUriForFile(
McContext,
应用程序_ID+“.provider”,
文件(it.path)
)
)
}
}.成功{
require().runOnUiThread{
如果(uriArrayList.size>0){
val intent=intent()
intent.action=intent.action\u发送\u多个
intent.putParcelableArrayListExtra(intent.EXTRA\u流,uriArrayList)
intent.flags=intent.FLAG\授予\读取\ URI\权限
intent.type=“image/*| application/pdf/*”
startActivity(Intent.createChooser(Intent,resources.getString(R.string.share)))
}
}
}
.失败{
Log.e(“共享失败”,it)
}
}
首先,您必须在应用程序清单文件中编写提供者代码,以便在android 7.0及以上版本上共享

<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>

此处提供程序路径为:

    <?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path name="/storage/emulated/0" path="."/>
    <root-path name="root" path="." />
    <files-path name="files" path="."/>
</paths>

对于那些在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)
    }
在应用程序标记内的清单中:

    <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>

在res-->xml-->提供程序_paths.xml下

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <files-path name="files" path="." />
    <external-path name="external_files" path="."/>
</paths>


myFilePath是上述示例中PDF文件的路径。在Android中,您可能会找到许多从SD卡中选择文件的解决方案。上述程序只是将文件路径作为输入,并显示设备上要共享文件的应用程序。例如,如果您选择gmail应用程序,它会附加文件并设置正文和主题文本。此外,我还需要设置一个
文件提供程序,这可能会有帮助:我仍然无法共享文件。当我尝试将其附加到gmail中时,它显示gmail中的toast出现错误。请提供帮助。感谢您提供一些信息,您的代码所做的工作可能会对提问者有所帮助。令人惊讶的-关于
文件提供程序
,我有一个问题:。您是否可以共享您的清单部分r文件提供者和提供者路径xml文件?这是清单部分,您可以在清单文件中尝试。xml部分是:@Oliver谢谢您的提问,可能是您的问题已通过上述解决方案解决了,如果没有,请告诉我。谢谢!您解决了我的问题。我使用了Uri.fromFile(exportPath))不再使用Uri.parse(“文件:/”+myFilePath),现在可以共享图像。谢谢您的回答。@YogeshNikamPatil很高兴它对您有所帮助。:)谢谢你!我曾面临这个问题,但在添加/storage/simulated/0后,该错误消失了。
    <?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path name="/storage/emulated/0" path="."/>
    <root-path name="root" path="." />
    <files-path name="files" path="."/>
</paths>
 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)
    }
    <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>
<?xml version="1.0" encoding="utf-8"?>
<paths>
    <files-path name="files" path="." />
    <external-path name="external_files" path="."/>
</paths>