Android sharing Files, by sending them via email o

2020-02-07 18:51发布

I have a list of files in my android app and I want to be able to get the selected items and send them via email or any other sharing app. Here is my code.

Intent sendIntent = new Intent();
                    sendIntent.setAction(Intent.ACTION_SEND);
                    sendIntent.putExtra(Intent.EXTRA_EMAIL, getListView().getCheckedItemIds());
                    sendIntent.setType("text/plain");
                    startActivity(sendIntent);

8条回答
疯言疯语
2楼-- · 2020-02-07 19:33

Read this article about Sending Content to Other Apps

Intent sendIntent = new Intent();

sendIntent.setAction(Intent.ACTION_SEND);

sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");

sendIntent.setType("text/plain");

startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
查看更多
我命由我不由天
3楼-- · 2020-02-07 19:35
File directory = new File(Environment.getExternalStorageDirectory() + File.separator + BuildConfig.APPLICATION_ID + File.separator + DIRECTORY_VIDEO);
            String fileName = mediaModel.getContentPath().substring(mediaModel.getContentPath().lastIndexOf('/') + 1, mediaModel.getContentPath().length());
            File fileWithinMyDir = new File(directory, fileName);
            if (fileWithinMyDir.exists()) {
                Uri fileUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", fileWithinMyDir);
                Intent intent = ShareCompat.IntentBuilder.from(this)
                        .setStream(fileUri) // uri from FileProvider
                        .setType("text/html")
                        .getIntent()
                        .setAction(Intent.ACTION_SEND) //Change if needed
                        .setDataAndType(fileUri, "video/*")
                        .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                startActivity(intent);
查看更多
▲ chillily
4楼-- · 2020-02-07 19:39

First you should define File Provider, see https://medium.com/@ali.dev/open-a-file-in-another-app-with-android-fileprovider-for-android-7-42c9abb198c1.

The code checks that a device contains applications which can receive the file, see How to check if an intent can be handled from some activity?.

fun sharePdf(file: File, context: Context) {
    val uri = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        Uri.fromFile(file)
    } else {
        try {
            FileProvider.getUriForFile(context, context.packageName + ".provider", file)
        } catch (e: Exception) {
            if (e.message?.contains("ProviderInfo.loadXmlMetaData") == true) {
                throw (Error("FileProvider is not set or doesn't have needed permissions"))
            } else {
                throw e
            }
        }
    }

    if (uri != null) {
        val intent = Intent()
        intent.action = Intent.ACTION_SEND
        intent.type = "application/pdf" // For PDF files.
        intent.putExtra(Intent.EXTRA_STREAM, uri)
        intent.putExtra(Intent.EXTRA_SUBJECT, file.name)
        intent.putExtra(Intent.EXTRA_TEXT, file.name)
        // Grant temporary read permission to the content URI.
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)

        // Validate that the device can open your File.
        val pm = context.packageManager
        if (intent.resolveActivity(pm) != null) {
            context.startActivity(Intent.createChooser(intent,
                context.getString(R.string.share_pdf)))
        }
    }
}
查看更多
走好不送
5楼-- · 2020-02-07 19:40

This is work for every single file!

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

}

Thanks Tushar-Mate!

查看更多
戒情不戒烟
6楼-- · 2020-02-07 19:42
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(exportPath));

also you can make zip file of all file and attach zip file for send multiple file in android

查看更多
可以哭但决不认输i
7楼-- · 2020-02-07 19:52

this is the code for sharing file in 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"));
}
查看更多
登录 后发表回答