Yes!
To share a file, you must create it before and send it!
Try this:
In the AndroidManifest.xml file add the write permission, so we can create the file before sending!
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
In your Activity:
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class SuaActivity extends AppCompatActivity {
private final String HTML = "<html> <body> <h1>Meu HTML compartilhado</h1> </body> </html>";
private final int REQUEST_PERMISSION = 9876;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cmd);
}
/**
* Vamos Criar um arquivo chamado shared.html para que possamos compartilhar este!
*/
public File writeToFile(String data) throws IOException {
FileOutputStream stream = null;
try {
File root = android.os.Environment.getExternalStorageDirectory();
File file = new File(root, "shared.html");
stream = new FileOutputStream(file, false);
stream.write(data.getBytes());
return file;
} finally {
if (null != stream) {
stream.close();
}
}
}
/**
* Vertifica se há permissão de escrita, caso não tenha, solicita!
* Este método é invocado para compartilhar o arquivo!
*/
private void checkPermission() {
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
shared();
} else {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSION);
}
}
/**
* Retorno da solicitação da permissão,
*/
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
int permissionCheck = ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION);
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
shared();
}else{
Toast.makeText(getApplicationContext(), "É necessário a permissão de escrita para compartilhar!", Toast.LENGTH_SHORT).show();
}
}
/**
* Compartilha o arquivo Html criado
*/
private void shared() {
try {
final File arquivo = writeToFile(HTML);
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/html");
final String filePath = String.format("file://%s", arquivo.getAbsolutePath());
final String sharingText = String.format("Compartilhando %s...", arquivo.getName());
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(filePath));
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, sharingText);
sharingIntent.putExtra(Intent.EXTRA_TEXT, sharingText);
startActivity(Intent.createChooser(sharingIntent, "Share using"));
} catch (final Exception e) {
e.printStackTrace();
}
}
}