Android Bitmap to File send with Intent - android

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

Related

Share image and text with Intent to Whats App

I am trying to share an image from imageview with text caption to whatsApp but the solutions I found online didn't seem to work for me.
View content = findViewById(R.id.posted_house);
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File root = Environment.getExternalStorageDirectory();
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
try
{
root.createNewFile();
FileOutputStream ostream = new FileOutputStream(cachePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
Intent txtIntent = new Intent(android.content.Intent.ACTION_SEND);
txtIntent .setType("image/*");
txtIntent .putExtra("message");
txtIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(cachePath));
startActivity(Intent.createChooser(txtIntent ,"Share"));
}
Here is code for share image and text in whatsapp..
View screenView = rootView.getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(this.getContentResolver(), bitmap, "Title", null);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
intent.putExtra(android.content.Intent.EXTRA_TEXT, "Your message");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(path));
try {
startActivity(Intent.createChooser(intent, "Share Screenshot"));
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "No App Available", Toast.LENGTH_SHORT).show();
}

Image only Shared with Whatsapp but not with other apps in Android

I am trying to share image using intent. Here is the method that i created
public void shareImg(int fileNum) //Consider fileNum=R.drawable.img
{
Uri uri= Uri.parse("android.resource://"
+ context.getPackageName() + "/" + fileNum);
Intent share=new Intent();
share.setAction(Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, uri);
share.putExtra(Intent.EXTRA_TEXT, "Sent Via ---");
Intent chooser= Intent.createChooser(share, "Share Via");
context.startActivity(chooser);
}
The image is shared properly with Whatsapp with caption. But when I try to share app with Gmail, Messenger, etc it gives error shown in Toast.
For eg.
Gmail says : Can't attach empty file
Messenger says : Failed to convert to image
You can share image using share intent, but you've to decode image to a localized Bitmap
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "Hey view/download this image");
String path = Images.Media.insertImage(getContentResolver(), loadedImage, "", null);
Uri screenshotUri = Uri.parse(path);
intent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "Share image via..."));
loadedImage is the Path of image
Here is the steps you can perform.
Step 1. First create bitmap from drawable
Drawable d = ImagesArrayList.get(0);
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
Step 2. Save Bitmap to File
FileOutputStream out = null;
String filename = Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg";
try {
out = new FileOutputStream(filename);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
// PNG is a lossless format, the compression factor (100) is ignored
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Step 3. Share this image File with fileurl. Share the Image same as you sharing gallery image.
Complete Answer
Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.xxxx); // your resource ID here
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/LatestShare.jpg";
OutputStream out = null;
File file=new File(path);
try {
out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
path=file.getPath();
Uri bmpUri = Uri.parse("file://"+path);
Intent shareIntent = new Intent();
shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setType("image/jpg");
startActivity(Intent.createChooser(shareIntent,"Share with"));

save screen contents as a image into internal memory and share with social media programatically

Below code will capture the screen and store it in SD card. I want Then it will send this file via sharable apps.I want to store it in internal memory of phone instead but I am unable to do that.Please help me for the same.
WebView view = (WebView) findViewById(R.id.webView1);
#SuppressWarnings("deprecation")
Picture picture = view.capturePicture();
Bitmap b = Bitmap.createBitmap( picture.getWidth(),
picture.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas( b );
picture.draw( c );
String filePath = Environment.getExternalStorageDirectory()
+ File.separator + "score.png";
File imagePath = new File(filePath);
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
if(fos!=null)
{
b.compress(CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
}
if(imagePath.exists())
{
sendMail(filePath);
}
else
{
Log.e("fie","file doesnt exist");
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
public void sendMail(String path) {
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
//emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
// new String[] { "youremail#website.com" });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"My Score in Mock Test");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
"PFA");
emailIntent.setType("image/png");
Uri myUri = Uri.parse("file://" + path);
emailIntent.putExtra(Intent.EXTRA_STREAM, myUri);
startActivity(Intent.createChooser(emailIntent, "share score card..."));
}
I tried to achieve this with below code found on stack overflow but it is not working. This code is not showing any exception when I debug through it but file is not creating to internal memory and so sending is failing.

Android save and share Image From SDCard

Hi I'm Trying to save a drawable as a bitmap to the SDCard and then share it using a share intent. but the problem i'm having is that its just not sharing the image does anyone know how to do this or where im going wrong?
heres what i have tried so far
getHelp.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
saveBitmapToExternalStorage(this, imageResourceFor Drawable, imageName);
File imageFile = new File("/sdcard/myfolder/"+ kitImagePath+".png");
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Title");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Subtitle");
shareIntent.putExtra(Intent.EXTRA_STREAM, bitmap); //optional//use this when you want to send an image
shareIntent.setType("image/png");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "send"));
}
});
public static void saveBitmapToExternalStorage(Context context, int imageResource, String imageName){
Bitmap bitmap=BitmapFactory.decodeResource(context.getResources(),imageResource);
//generate file
File dir = new File (Environment.getExternalStorageDirectory(), "/myfolder");
File f = new File(dir, String.format(imageName + ".png"));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 , bos);
byte[] bitmapdata = bos.toByteArray();
FileOutputStream fos;
try {
fos = new FileOutputStream(f);
fos.write(bitmapdata);
fos.flush();
fos.close();
bos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Instead of using Bitmap use its URI to share.
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Title");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Subtitle");
Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+ kitImagePath+".png"));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri); //optional//use this when you want to send an image
shareIntent.setType("image/png");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "send"));
Also as +Der Golem said. use Environment.getExternalStorageDirectory().getAbsolutePath() to get External storage path.
Add the following Permission in your manifest file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Issue on sending MMS in Android version 4.0.3

Hi I want to send an MMS through my application.For that my code is
void sendMMS()
{
try
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Drawable drawable = imageView.getDrawable();
Bitmap bitmapPicked = ((BitmapDrawable) drawable).getBitmap();
bitmapPicked.compress(CompressFormat.JPEG, 75, bos);
byte[] image = bos.toByteArray();
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "test.jpg");
file.createNewFile();
// write the bytes in file
FileOutputStream fo = new FileOutputStream(file);
fo.write(image);
Log.i(TAG, "image = " + image);
Intent intentEmail = new Intent(Intent.ACTION_SEND);
intentEmail.setType("text/plain");
String[] recipients = new String[] { "" };
intentEmail.putExtra(Intent.EXTRA_EMAIL, recipients);
intentEmail.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
intentEmail.putExtra(Intent.EXTRA_TEXT, "body of email");
intentEmail.putExtra("sms_body", "body of sms");
intentEmail.setType("image/jpeg");
intentEmail.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file.getAbsolutePath()));
startActivity(intentEmail);
} catch (android.content.ActivityNotFoundException ex)
{
Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
ex.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
}
}
When users click on a button, so this method is called & it shows a list of available options to perform this action.So for sending MMS user will have to select "Messaging" option.
Although this works fine for Android version 2.3 but when I run the app on version 4.0.3 then in the list of available options it does not show "Messaging" option.Which is must for sending MMS.
And when I remove the lines
intentEmail.setType("image/jpeg");
intentEmail.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file.getAbsolutePath()));
then the list shows the "Messaging" option but I can not remove it.
I am really not getting what is the problem with it or may I will have to add something more for version 4.0.3 .
Please help.
Ok I solved the issue.
My new code is
void sendMMS()
{
try
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Drawable drawable = imageProduct.getDrawable();
Bitmap bitmapPicked = ((BitmapDrawable) drawable).getBitmap();
bitmapPicked.compress(CompressFormat.JPEG, 75, bos);
byte[] image = bos.toByteArray();
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "test.jpg");
file.createNewFile();
// write the bytes in file
FileOutputStream fo = new FileOutputStream(file);
fo.write(image);
Log.i(TAG, "image = " + image);
Intent intentMMS = new Intent(Intent.ACTION_SEND);
intentMMS.setType("image/jpg");
intentMMS.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
intentMMS.putExtra("sms_body", messageFacebook);
intentMMS.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivity(intentMMS);
} catch (android.content.ActivityNotFoundException ex)
{
Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
ex.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
}
}

Categories

Resources