I'm working on an Android application that should allow the users to share their content via Gmail. I'm using android version 2.2(Froyo).
The problem is that I can't find any working solution for this, I tried almost everything ,but with no luck.
this is the code I'm using:
Intent sharingIntent = new Intent(Intent.ACTION_SEND);;
sharingIntent.setType("application/zip");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
getString(R.string.share_subject));
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, getString(R.string.share_body));
String zipFile = FileProvider.URI_AUTHORITY + File.separator + mItemSelected.getLibraryName() + File.separator + mItemSelected.getZipFileName();
sharingIntent.putExtra(Intent.EXTRA_STREAM, android.net.Uri.parse(zipFile));
startActivity(Intent.createChooser(sharingIntent, (getString(R.string.share_chooser))));
}
The problem in this case is that the Gmail app, for no obvious reason, is replacing the mime type of the file, and show the file as text/html, and then my application is not shown in the application list that can deal with this kind of file. Another restriction is that I don't want to use text/html in my intent filter, because I want it to be focused as much as possible, and if it were possible I would define my own mime type...
I did a little research and found this question, but with no answers...
More mime types I tried:
application/x-compressed, application/x-zip-compressed
multipart/x-zip and application/octet-stream
Is there any solution for this problem ??
Thanks.
after a lot of trouble, I discovered that Gmail, launched via Intent, does not like attachments whose prefix is .zip.
So, I succeeded in sending the attachments after renaming it ".vip".
Here is a piece of code (outFile is a zipped file renamed as ".vip"):
enter
private void sendMail(File outFile) {
Uri uriToZip = Uri.fromFile(outFile);
String sendText = "Dear friend,\n\n...";
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] { "checcodotti#gmail.com" });
sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, sendText);
sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Log of the test " + expFilename);
// sendIntent.setType("image/jpeg");
// sendIntent.setType("message/rfc822");
sendIntent.setType("*/*");
sendIntent.putExtra(android.content.Intent.EXTRA_STREAM, uriToZip);
startActivity(Intent.createChooser(sendIntent, "Send Attachment !:"));
}
Please let me know if it helps.
Regards
FD
I improve my previous answer for the part concerning "zipping". Now the are no problems with .zip attachments sent via GMail or whatever. Try this:
{
int lung;
FileInputStream in;
FileOutputStream out;
byte[] buffer = new byte[DIM_BUFFER];
// compress the file to send
String inPath = ctx.getApplicationContext().getFilesDir().getAbsolutePath();
outFile = new File(outPath,TestEdit.ZIPNAME);
// outFile = new File(outPath,filename + ".vip");
in = new FileInputStream(inFile);
ZipEntry entry = new ZipEntry(filename + ".csv");
try{
out = new FileOutputStream(outFile);
// GZIPOutputStream zos;
ZipOutputStream zos;
zos = new ZipOutputStream(new BufferedOutputStream(out) );
zos.putNextEntry(entry);
try {
while ((lung=in.read(buffer)) > 0) {
Log.v(TAG, "Lunghezza di in=" + lung + ". Lungh di buffer=" + buffer.length );
if (buffer.length == lung) {
zos.write(buffer);
} else {
// Gestione del caso in cui il buffer non sia pieno
for (int b = 0; b < lung; b++) {
zos.write(buffer[b]);
}
}
}
} finally {
zos.closeEntry();
zos.close();
in.close();
out.close();
}
}
}
Related
I have an issue with my sharing option.
The option was perfectly well working (and I have saved it before trying some modifications) but I have tried to modify something and I cannot reach my goal).
The purpose is : click on the option in a menu, click on share, if the folder "test folder" doesn't exists in the "MUSIC" folder, create it, if it already exists, copie the sound, put it in the folder previously created and then use it as an extra in a SEND intent.
case R.id.share:
if (isStoragePermissionGranted()) {
File outputFile = new File("");
InputStream is = getResources().openRawResource(((Sound) adapter.getItem(index)).getMpsound());
try {
File outputDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC), "Test folder");
outputFile = new File(outputDir, getResources().getResourceEntryName(((Sound) adapter.getItem(index)).getMpsound()) + ".mp3");
if (outputDir.exists()) {
byte[] buffer = new byte[is.available()];
is.read(buffer);
OutputStream os = new FileOutputStream(outputFile);
os.write(buffer);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/*");
share.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", outputFile));
share.putExtra(Intent.EXTRA_TEXT, "\"" + ((Sound) adapter.getItem(index)).getTitle_show() + "\" shared by my app");
startActivity(Intent.createChooser(share, "Share Sound File"));
} else {
outputDir.createNewFile(); //plus add code as below to share the sound after creating the folder
}
} catch (IOException e) {
e.printStackTrace();
}
}
I'm not able to create the folder and if I create it manually, the code is working well because the mp3 appears in the folder, and right after I have an error that says :
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Music/my app/sound_test.mp3
So... the app is able to access the folder to create the sound but for an unknown reason, I have this issue.
What did I do wrong ?
So, for the ones who are coming in few days, months, years with the same problem :
As CommonsWare said it, the issue in accessing the folder was from the FileProvider. After configure it properly with the good path, I had no more issue.
For the part concerning the creation of a new folder, .createNewFile() doesn't seems to be the right way. It's necessarry to use .mkdir()
And to end it, the part concerning the delete of the files in the folder, you will have your answer there :
File dir = new File(Environment.getExternalStorageDirectory()+"Dir_name_here");
if (dir.isDirectory())
{
String[] children = dir.list();
for (int i = 0; i < children.length; i++)
{
new File(dir, children[i]).delete();
}
}
Source : How to delete all files in a directory java android app? Current code is deleting the that folder
I've asked a similar question before but I figured it out myself. But I now have a new problem, my device updated to android 8.0 and now email intent is not working. I don't want to downgrade my device if possible.
private void sendScreen() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".png";
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.PNG, quality, outputStream);
outputStream.flush();
outputStream.close();
File filelocation = new File(MediaStore.Images.Media.DATA + mPath);
Uri myUri = Uri.parse("file://" + filelocation);
final Intent emailIntent = new Intent(Intent.ACTION_SEND);
// set the type to 'email'
emailIntent .setType("vnd.android.cursor.dir/email");
String to[] = {"Enter your email address"};
emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
// the attachment
emailIntent.putExtra(Intent.EXTRA_STREAM, myUri);
// the mail subject
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Journey : " + now );
startActivity(Intent.createChooser(emailIntent , "Select your preferred email app.."));
} catch (Throwable e) {
// Several error may come out with file handling or DOM
e.printStackTrace();
}
}
This is my code which worked perfectly on android 7.0. The code takes a screenshot, time stamps it, saves it to the local storage and then attached it to an email app of the users choice. Anyone have a solution? Thanks
This is my code which worked perfectly on android 7.0
It should have crashed with a FileUriExposedException.
now email intent is not working
You did not explain what "is not working" means. I am going to guess that you are crashing with a FileUriExposedException. A file Uri — whether via Uri.fromFile() or your Uri.parse("file://"+...) approach — is not usable on Android 7.0+ by default. Switch to using FileProvider and getUriForFile().
Developing Android email plugin for Unity. I have a screenshot in the files/ folder of the app, I want to attach to mail. As it turned out, I cannot attach from there directly. I implemented a FileProvider, but it turned out that it exist only above 4.0.
So I implemented the suggested workaround, to save it to external storage, then attach from there. Saving seems work, even reading seems work, but still, Gmail says "Can't attach empty file". Also When launching email intent, I have an error message, like:
E/HwEmailTag( 7327): AttachmentUtilities->inferMimeTypeForUri->Unable to determine MIME type for uri=/storage/emulated/0/com.eppz.plugins_screenshot.jpg
I tried application/image, image/jpg as intent.setType(), still the same, while Gmail says the file is empty.
Is this something with emulated external storage /storage/emulated/0/? The device has no SD card, but I've read that getExternalStorage() returns a shared / public place for files in such cases either.
It should work. Should I remove dots from filename? Hope not. Here's the corresponding code:
String saveImageAtPathToExternalStorage(String imagePath)
{
Log.i(TAG, "saveImageAtPathToExternalStorage(...)");
// Create bitmap.
File imageFile = new File(imagePath);
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), new BitmapFactory.Options());
// Output.
String outputFileName = _unityPlayerActivity.getPackageName()+"_screenshot.jpg";
String externalStorageDirectory = Environment.getExternalStorageDirectory().toString();
File outputImageFile = new File(externalStorageDirectory, outputFileName);
String outputImagePath = outputImageFile.getAbsolutePath();
if (outputImageFile.exists()) outputImageFile.delete(); // Delete if existed
try
{
// Write JPG.
FileOutputStream outputStream = new FileOutputStream(outputImageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();
Log.i(TAG, "Image written to `"+outputImagePath+"`");
}
catch (Exception e)
{ e.printStackTrace(); }
// Return with output path.
return outputImagePath;
}
public void openMailComposer(String to, String subject, String body, int isHTML, String attachmentImagePath)
{
Log.i(TAG, "openMailComposer(...)");
// Attachment image.
File attachmentImageFile = new File(attachmentImagePath);
if (attachmentImageFile.exists() == false)
{
Log.i(TAG, IMAGE_NOT_FOUND);
SendUnityMessage(OPEN_MAIL_COMPOSER_CALLBACK_METHOD_NAME, IMAGE_NOT_FOUND);
return;
}
// Save to external first.
String externalImagePath = saveImageAtPathToExternalStorage(attachmentImagePath);
final Uri externalImageUri = Uri.parse(externalImagePath);
Log.i(TAG, "externalImageUri `"+externalImageUri+"`");
// Intent.
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_EMAIL, to);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, body);
if (isHTML == 1) intent.putExtra(Intent.EXTRA_HTML_TEXT, body);
// Attach.
intent.putExtra(Intent.EXTRA_STREAM, externalImageUri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(Intent.createChooser(intent, "Send email"), OPEN_MAIL_COMPOSER_REQUEST_CODE);
}
I'm relatively new to Android development, and really want to believe, but having all this hassle compared to having a single line for this in iOS is quiet distressing.
I'm running this on a Huawei MediaPad (TT1 7.0), Android 4.4.2, and I want it to run about Android 2.3+ basically (why I refused using FileProvider earlier).
This question has been posted before, but there was no clear or accepted answer and all of the solutions provided that were supposed to "work" didn't for me. See here: Gmail 5.0 app fails with "Permission denied for the attachment" when it receives ACTION_SEND intent
I have an app which builds up data in a text file and needs to send the text file along in an email, automatically attaching it. I have tried many ways to get this to attach, and it apparently works for Gmail 4.9 and below but 5.0 has some new permission features disabling it from doing what I wish.
Intent i = new Intent(Intent.ACTION_SEND);
String to = emailRecipient.getText().toString();
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
i.putExtra(Intent.EXTRA_SUBJECT, "Pebble Accelerometer Data");
i.putExtra(Intent.EXTRA_TEXT, "Attached are files containing accelerometer data captured by SmokeBeat Pebble app.");
String[] dataPieces = fileManager.getListOfData(getApplicationContext());
for(int i2 = 0; i2 < dataPieces.length; i2++){
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(getApplicationContext().getFilesDir() + File.separator + dataPieces[i2])));
}
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(getApplicationContext().getFilesDir() + File.separator + fileManager.getCurrentFileName(getApplicationContext()))));
Log.e("file loc", getApplicationContext().getFilesDir() + File.separator + fileManager.getCurrentFileName(getApplicationContext()));
try {
startActivity(Intent.createChooser(i, "Send Email"));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(Main.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
The datapieces might be empty yes but the current file line below the for loop is always reliable and always attaches something.
I have tried changing
Uri.fromFile()
to
Uri.parse()
When I do that, it attaches, but Gmail then crashes and when I check the logcat it's because of a null pointer. This is most likely because Gmail has no access to the file and therefore results as null.
I've also tried using
getCacheDir()
instead of
getFilesDir()
and it has the same outcome.
What am I doing wrong here, and how should I go about fixing it? Some example code would be really, really handy because I am new to Android development and explaining what I need to do without some sort of push off probably won't end up helping.
Thanks a lot.
Alright guys. Took a break and came back, figured it out.
Here's how it works, you need to have write/read permissions to external storage, so add these permissions to your manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Then, your file has to be copied from your app's internal storage directory into the app's external directory. I recommend you use internal storage, and that's what I'm doing here so you can figure out SD cards yourself.
Here is the block of code that does the magic. Logs are included but you can remove them by all means.
public void writeToExternal(Context context, String filename){
try {
File file = new File(context.getExternalFilesDir(null), filename); //Get file location from external source
InputStream is = new FileInputStream(context.getFilesDir() + File.separator + filename); //get file location from internal
OutputStream os = new FileOutputStream(file); //Open your OutputStream and pass in the file you want to write to
byte[] toWrite = new byte[is.available()]; //Init a byte array for handing data transfer
Log.i("Available ", is.available() + "");
int result = is.read(toWrite); //Read the data from the byte array
Log.i("Result", result + "");
os.write(toWrite); //Write it to the output stream
is.close(); //Close it
os.close(); //Close it
Log.i("Copying to", "" + context.getExternalFilesDir(null) + File.separator + filename);
Log.i("Copying from", context.getFilesDir() + File.separator + filename + "");
} catch (Exception e) {
Toast.makeText(context, "File write failed: " + e.getLocalizedMessage(), Toast.LENGTH_LONG).show(); //if there's an error, make a piece of toast and serve it up
}
}
Encountered the same attachment denied. Permissions in manifest did not have any effect, rather do not have an effect any more since API 23. Finally solved it as follows.
1st need to check and grant permissions on run-time, I did it in my main activity:
public static final int MY_PERMISSIONS_REQUEST_READ_STORAGE=10001;
private void checkPermission(){
if (this.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (this.shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)) {
// Show an explanation to the user asynchronously
} else {
// No explanation needed, we can request the permission.
this.requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_STORAGE);
}
}
}
Now when sending, create a file in PUBLIC directory (tried saving to my app folder - same denial problem)
public File createFile(){
String htmlStr="<!DOCTYPE html>\n<html>\n<body>\n<p>my html file</p></body></html>";
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "aimexplorersummary.html");
try {
FileWriter writer = new FileWriter(file ,false);
writer.write(htmlStr);
}
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
return file;
}
Now compose sending intent and putExtra with uri to your file which is in public storage that user must grant permissions to and that causes no problem now
public void send(){
Intent intentSend = new Intent(android.content.Intent.ACTION_SEND);
intentSend.setType("text/html");
File file = createFile();
if(file!=null){
intentSend.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
}
startActivity(Intent.createChooser(intentSend, "Send using:"));
}
I've seen several exemples but still don't get why, when I'm editing the mail I see the .xml attached but when I receive ther's no attachment!
Here is my code
File f = new File("data/data/xxx/files/xxx.xml");
Boolean b1 = f.exists();
Boolean b2 = f.canRead();
if (b1 && b2) {
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_EMAIL, "");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" +
f.getAbsolutePath()));
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "XXX");
sendIntent.putExtra(Intent.EXTRA_TEXT, R.string.mail_body);
startActivity(Intent.createChooser(sendIntent, "Email:"));
} else {
...
Ah, only a detail...when I choose the app to send there is no subject or body, even if I wrote putExtra(Intent.EXTRA_SUBJECT) and putExtra(Intent.EXTRA_TEXT), but that's a detail...
Edit: I just debuged my intent: it says "NOT CACHED" in value of the stream, how to solve it?
You can't attach a file from internal storage directly for some security purpose, hence first you have to copy that file from internal to external directory and then mail after that if you want you can delete that file from external storage in onActivityResult() method.
Here's a code :
private File copyFileToExternal(String fileName) {
File file = null;
String newPath = Environment.getExternalStorageState()+"/folderName/";
try {
File f = new File(newPath);
f.mkdirs();
FileInputStream fin = openFileInput(fileName);
FileOutputStream fos = new FileOutputStream(newPath + fileName);
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = fin.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fin.close();
fos.close();
file = new File(newPath + fileName);
if (file.exists())
return file;
} catch (Exception e) {
}
return null;
}
Method to Email:
private void sendEmail(String email) {
File file = new File(Environment.getExternalStorageState()+"/folderName/" + fileName+ ".xml");
Uri path = Uri.fromFile(file);
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("application/octet-stream");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
String to[] = { email };
intent.putExtra(Intent.EXTRA_EMAIL, to);
intent.putExtra(Intent.EXTRA_TEXT, message);
intent.putExtra(Intent.EXTRA_STREAM, path);
startActivityForResult(Intent.createChooser(intent, "Send mail..."),
1222);
}
and then
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1222) {
File file = new File(Environment.getExternalStorageState()+"/folderName/" + fileName+ ".xml");
file.delete();
}
}
Call this method like this:
copyFileToExternal(filename + ".xml");
sendEmail(EmailId);
I've seen several exemples but still don't get why, when I'm editing the mail I see the .xml attached but when I receive ther's no attachment!
First, third-party apps cannot read internal storage of your app.
Second, that might not be the right path to internal storage of your app. Never hardcode paths. Your app will fail for secondary accounts and restricted profiles on Android 4.2 tablets, for example. Always use a method, like getFilesDir(), to get at your portion of internal storage.
You will need to either copy your file to external storage, or better yet, use FileProvider to serve up your file from internal storage via a content:// Uri.