Android sharing Files, by sending them via email or other apps

99封情书 提交于 2019-11-28 19:42:49

问题


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

回答1:


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



回答2:


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




回答3:


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!




回答4:


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



回答5:


Use ACTION_SEND_MULTIPLE for delivering multiple data to someone

intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, arrayUri);
intent.setType("text/plain");
startActivity(intent);

The arrayUri is the Array List of Uri of files to Send.




回答6:


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



回答7:


Here is an example to share or save a text 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()));
    }
}



回答8:


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


来源:https://stackoverflow.com/questions/17985646/android-sharing-files-by-sending-them-via-email-or-other-apps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!