Android attach image to email doesn't work - android

I'm trying to send email from my application with it's logo.
But I receive the email when the attachment in string format(should be png).
My code:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("application/image");
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, getString(R.string.fb_share_description));
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://my.package/" + R.drawable.ic_launcher));
Intent chooser = Intent.createChooser(intent, "share");
startActivity(chooser);
What should I do?

You cannot attach files to an email from your internal resources. You must copy it to a commonly accessible area of the storage like the SD Card first.
InputStream in = null;
OutputStream out = null;
try {
in = getResources().openRawResource(R.drawable.ic_launcher);
out = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), "image.png"));
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e) {
Log.e("tag", e.getMessage());
e.printStackTrace();
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
}
//Send the file
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/html");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "File attached");
Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "image.png"));
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
This is required as the resources you bundle with your app are read only and sandboxed to your application. The URI that the email client receives is one it cannot access.

Related

share apk from my app doesnt work

i want to share my apllication's apk to another device, and this is not working.
this code is from the internet.. how can i fix this and share my app from my app
private void shareApplication() {
ApplicationInfo app = getApplicationContext().getApplicationInfo();
String filePath = app.sourceDir;
Intent intent = new Intent(Intent.ACTION_SEND);
// MIME of .apk is "application/vnd.android.package-archive".
// but Bluetooth does not accept this. Let's use "*/*" instead.
intent.setType("*/*");
// Append file and send Intent
File originalApk = new File(filePath);
try {
//Make new directory in new location
File tempFile = new File(getExternalCacheDir() + "/ExtractedApk");
//If directory doesn't exists create new
if (!tempFile.isDirectory())
if (!tempFile.mkdirs())
return;
//Get application's name and convert to lowercase
tempFile = new File(tempFile.getPath() + "/" + getString(app.labelRes).replace(" ","").toLowerCase() + ".apk");
//If file doesn't exists create new
if (!tempFile.exists()) {
if (!tempFile.createNewFile()) {
return;
}
}
//Copy file to new location
InputStream in = new FileInputStream(originalApk);
OutputStream out = new FileOutputStream(tempFile);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
System.out.println("File copied.");
//Open share dialog
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(tempFile));
startActivity(Intent.createChooser(intent, "Share app via"));
} catch (IOException e) {
e.printStackTrace();
}
}
it`s send the apk in same weight as the generating of android atudio but when install the app crash.

Android Bitmap to File send with Intent

I have this code that gets an image from database as Bitmap and then writes this to a file and sends it with e-mail. This code works great.
I am trying to write this to a textfile that actually shows the picture.
Is this possible?
Do I need to write this to pdf file if I want a file that shows the image?
Here's my code
public void createBild(long x, String pathToFile, String fileName) {
Product product = dbHandler.findProductbyId(x);
Bitmap pic = BitmapFactory.decodeByteArray(dbHandler.fetchSingle(x), 0,
dbHandler.fetchSingle(x).length);
// create a file to write bitmap data
Bitmap bitmap = pic;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /* ignored for PNG */, bos);
byte[] bitmapdata = bos.toByteArray();
File f = new File(pathToFile + "/"+fileName+".bmp");
try {
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
fos.write(bitmapdata);
fos.flush();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Uri path = Uri.fromFile(f);
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("image/*");
i.putExtra(Intent.EXTRA_EMAIL, new String[] { "test#live.se" });
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT, "body of email");
i.putExtra(Intent.EXTRA_STREAM, path);
try {
startActivity(Intent.createChooser(i, "Share"));
} catch (android.content.ActivityNotFoundException e) {
Toast.makeText(VisaData.this,
"There are no email clients installed.", Toast.LENGTH_SHORT)
.show();
}
}
Why don't you send it as a .png itself.
intent.setType("image/png");

Android: Image transfer using bluetooth or email

I'm new to Android Development.
I'm trying to make an application where I load an image, edit and save it, and transfer the image through bluetooth and email.
It seems like Android has a built in tools that I can use to easily implement bluetooth feature, But I couldn't find any easy and straight-forward tutorials yet.
Can someone help me and give me an example or links to a good tutorial regarding bluetooth feature or email feature?
Please help!
Thanks
use this below script
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("image/jpeg");
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Photo");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/dcim/Camera/filename.jpg"));
sendIntent.putExtra(Intent.EXTRA_TEXT, "Enjoy the photo");
startActivity(Intent.createChooser(sendIntent, "Email:"));
With this code, first save file on sdCad, then send it with Bt, email or ...
send.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
File newSoundFile = new File("/sdcard/101Ringtone.ogg");
Uri mUri = Uri.parse("android.resource://" + getPackageName() + "/"+ RingtoneRaw);
ContentResolver mCr = getContentResolver();
AssetFileDescriptor soundFile;
try {
soundFile= mCr.openAssetFileDescriptor(mUri, "r");
} catch (FileNotFoundException e) {
soundFile=null;
}
try {
byte[] readData = new byte[1024];
FileInputStream fis = soundFile.createInputStream();
FileOutputStream fos = new FileOutputStream(newSoundFile);
int i = fis.read(readData);
while (i != -1) {
fos.write(readData, 0, i);
i = fis.read(readData);
}
fos.close();
} catch (IOException io) {
}
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("audio/ogg");
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(newSoundFile));
startActivity(Intent.createChooser(i, "Share ringtone via:"));
}
});

Sending picture in Whatsapp

I want to send a picture in Whatsapp. My App starts when I select the Image chooser in Whatsapp. How can I send the result of the Intent back to whatsapp?
I use the following Code:
// on button press
String path = SaveCache(R.drawable.pic_1);
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + path));
}
}
private String SaveCache(int resID) {
String path = "";
try {
InputStream is = getResources().openRawResource(resID);
File cacheDir = context.getExternalCacheDir();
File downloadingMediaFile = new File(cacheDir, "abc.jpg");
byte[] buf = new byte[256];
FileOutputStream out = new FileOutputStream(downloadingMediaFile);
while (true) {
int rd = is.read(buf, 0, 256);
if (rd == -1 || rd == 0)
break;
out.write(buf, 0, rd);
}
is.close();
out.close();
return downloadingMediaFile.getPath();
} catch (Exception ex) {
ex.printStackTrace();
}
return path;
}
I was able to send image using this code
Uri uri = Uri.parse("android.resource://com.example.test/drawable/image_1");
sharingIntent.setType("image/jpg");
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));

How to attach image in email from drawable in android

I am attaching an image from drawable folder and sending it in a email.
when I send it from default email client.Image extension(.png) is missing in attachment
and and also the file name is changed itself.
I want t send image with default name(as in drawable) and with .png extension.
this is my code.
Intent email = new Intent(Intent.ACTION_SEND);
email.setType("image/png");
email.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
email.putExtra(Intent.EXTRA_SUBJECT, "Hey! ");
email.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://"+ getPackageName() + "/" + R.drawable.ic_launcher));
startActivity(Intent.createChooser(email, "Sending........"));
Please suggest me what is worng in this code
thanks.
You can only attach an image to mail if the image is located in the SDCARD.
So, you'll need to copy the image to SD and then attach it.
InputStream in = null;
OutputStream out = null;
try
{
in = getResources().openRawResource(R.raw.ic_launcher);
out = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), "image.png"));
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
}
catch (Exception e)
{
Log.e("tag", e.getMessage());
e.printStackTrace();
}
private void copyFile(InputStream in, OutputStream out) throws IOException
{
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1)
{
out.write(buffer, 0, read);
}
}
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/html");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "File attached");
Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "image.png"));
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailIntent, "Send mail..."));

Categories

Resources