I would like to send a Bitmap and some text by clicking on a button in a fragment and I would like to let the user choose the contact/number in his messaging app.
How I could do that ?
EDIT :
After trying something, I got an error in the message app (Messenger on the emulator) :
Messenger failed to load attachment.
Here is the code I used :
String path = Environment.getExternalStorageDirectory().getPath() + "/Improov/LatestShare.png";
File file = new File(path);
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Uri photoURI = FileProvider.getUriForFile(getContext(), getContext().getApplicationContext().getPackageName() + ".com.example.mous.improov_flash", file);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra("sms_body", message);
intent.putExtra(Intent.EXTRA_STREAM, photoURI);
intent.setType("image/jpeg");
getActivity().startActivity(intent);
If you like to send MMS,
First, you should store your bitmap in sd card, then use the following intent to send the MMS,
Get the Uri for your stored bitmap
Uri uri = [uri for your stored bitmap];
Intent intent = new Intent(Intent.ACTION_SEND);
inetnt.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra("sms_body", [Text to be send]);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/jpeg");
getActivity.startActivity(intent);
For more detail read this official doc
Related
I want to send image from my app via messenger. I was looking on Stack Overflow and I found answer which works for WhatsApp. When I tried to change "com.whatsapp" to "com.facebook.orca", it stops working. Here is my code:
public void shareImageMessenger() {
Bitmap adv = BitmapFactory.decodeResource(getResources(), R.drawable.koza);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
adv.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "temporary_file_1.jpg");
try {
f.createNewFile();
new FileOutputStream(f).write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM,
Uri.parse( Environment.getExternalStorageDirectory()+ File.separator+"temporary_file_1.jpg"));
share.setPackage("com.facebook.orca");
startActivity(Intent.createChooser(share, "Share Image"));
}
After spending a lot of time on this:
Check if permissions are given. Then:
Step 1: Create ImageView of the image you want to in the activity and then convert it itno bitmap
ImageView imageView = findViewById(R.id.image);
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
//save the image now:
saveImage(bitmap);
//share it
send();
Step 2: Store the image in internal folder:
private static void saveImage(Bitmap finalBitmap) {
String root = Environment.getExternalStorageDirectory().getAbsolutePath();
File myDir = new File(root + "/saved_images");
Log.i("Directory", "==" + myDir);
myDir.mkdirs();
String fname = "Image-test" + ".jpg";
File file = new File(myDir, fname);
if (file.exists()) file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Step 3: Send the saved image:
public void send() {
try {
File myFile = new File("/storage/emulated/0/saved_images/Image-test.jpg");
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext = myFile.getName().substring(myFile.getName().lastIndexOf(".") + 1);
String type = mime.getMimeTypeFromExtension(ext);
Intent sharingIntent = new Intent("android.intent.action.SEND");
sharingIntent.setType(type);
sharingIntent.putExtra("android.intent.extra.STREAM", Uri.fromFile(myFile));
startActivity(Intent.createChooser(sharingIntent, "Share using"));
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
Now after sending you can delete the saved image if you don't want it in your storage. Check other link to do that.
Referring your linked post,You could modify the share intent.
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///assets/epic/adv.png"));
this.startActivity(Intent.createChooser(share, "share_via"));
The intent launches the apps which handles Intent.ACTION_SEND. If you want specific app to be respond, make sure you are aware of the package name and you need set package name share.setPackage("");
I'm trying to include image sharing in my application and everything is working but the share chooser takes long time to appear on devices
here is what i'm doing:
I have ImageView "items_details_image" and I want to share its image to be able to send it through whatsapp and other apps
void ShareImg() {
try {
Uri bmpUri = getLocalBitmapUri(items_details_image);
if (bmpUri != null) {
// Construct a ShareIntent with link to image
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setType("image/*");
// Launch sharing dialog for image
startActivity(Intent.createChooser(shareIntent, "Share Image"));
} else {
// ...sharing failed, handle error
}
} catch (Exception e) {
}
}
here is how I get bitmap from Image URL
public Uri getLocalBitmapUri(ImageView imageView) {
// Extract Bitmap from ImageView drawable
Drawable drawable = imageView.getDrawable();
Bitmap bmp = null;
if (drawable instanceof BitmapDrawable){
bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
} else {
return null;
}
// Store image to default external storage directory
Uri bmpUri = null;
try {
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
file.getParentFile().mkdirs();
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = Uri.fromFile(file);
} catch (IOException e) {
e.printStackTrace();
}
return bmpUri;
}
I don't know why it is taking longer than usual comparing it with other apps on the same device such as gallery or whatever
You are doing disk I/O on the main application thread, in getLocalBitmapUri(). This will freeze your UI as long as it takes to write your bitmap to disk.
My activity has an imageview, edittext with user entered email address and a send button. On send button activity I have email message, subject and recipient name ready but I also want to add the image which has been contained by imageview.
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setData(Uri.parse("mailto:"));
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_EMAIL, receivers);
intent.putExtra(Intent.EXTRA_SUBJECT, "My subject");
intent.putExtra(Intent.EXTRA_TEXT, "hello wats up");
intent.putExtra(Intent.EXTRA_STREAM, image i want to send);
startActivity(intent);
obviously the last putExtra line will give me error stating they want string but I am passing imageview. Guide me please how can I include my imageview in this email body. (Not as an attachment but in message body with message text).
Many thank you in advance.
You've to pass fileUri as the second argument. Like this.
intent.putExtra(Intent.EXTRA_STREAM, fileUri);
Try out as below:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/*");
Uri uri = Uri.parse("android.resource://your package name/"+R.drawable.ic_launcher);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.putExtra(android.content.Intent.EXTRA_EMAIL,recipients);
shareIntent.putExtra(Intent.EXTRA_TEXT, message);
startActivity(Intent.createChooser(shareIntent, "Send your image"));
EDITED:
Declare the File variable like below
File pic;
In your OnActivityResult() apply changes as below:
Bundle ext = data.getExtras();
bmpEmail = (Bitmap)ext.get("data");
try {
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()){
pic = new File(root, "pic.png");
FileOutputStream out = new FileOutputStream(pic);
bmpEmail.compress(CompressFormat.PNG, 100, out);
out.flush();
out.close();
}
} catch (IOException e) {
Log.e("BROKEN", "Could not write file " + e.getMessage());
}
And in your send email code add the line
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic));
try this
ImageView iv = (ImageView) findViewById(R.id.splashImageView);
Drawable d =iv.getBackground();
BitmapDrawable bitDw = ((BitmapDrawable) d);
Bitmap bitmap = bitDw.getBitmap();
File mFile = savebitmap(bitmap);
and then
Uri u = null;
u = Uri.fromFile(mFile);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("image/*");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Hello...");
// + "\n\r" + "\n\r" +
// feed.get(Selectedposition).DETAIL_OBJECT.IMG_URL
emailIntent.putExtra(Intent.EXTRA_TEXT, "Your tsxt here");
emailIntent.putExtra(Intent.EXTRA_STREAM, u);
startActivity(Intent.createChooser(emailIntent, "Send email..."));
and savebitmap method
private File savebitmap(Bitmap bmp) {
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
OutputStream outStream = null;
File file = new File(extStorageDirectory, temp + ".png");
if (file.exists()) {
file.delete();
file = new File(extStorageDirectory, temp + ".png");
}
try {
outStream = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return file;
}
Intent.EXTRA_STREAM, image i want to send);
As soon as you use EXTRA_STREAM you are adding an attachment.
If you want to have your image in the body of the email you should send a html mail. Not a plain text mail.
So I take a screenshot of my activity, then save it to "screenshot.png". When trying to share it via, let's say, Whatsapp, I get an error: "could not send image". Same with gmail, pushbullet and basically all other apps. Therefore, I conclude that the file somehow exists, but it is either empty, or messed up... I don't know, honestly.
Here's the code:
Taking the activity screenshot:
public Bitmap takeScreenshot() {
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}
Saving the screenshot:
public void saveBitmap(Bitmap bitmap) {
FileOutputStream fos;
String filePath = getApplicationContext().getFilesDir().getPath().toString() + "/screenshot.png";
File f = new File(filePath);
try {
fos = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
And finally, the sharing itself:
if (id == R.id.action_share){
Bitmap bitmap = takeScreenshot();
saveBitmap(bitmap);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("screenshot.png"));
startActivity(Intent.createChooser(share, "Teilen via"));
}
Where is my error? I don't get any errors in logcat, but I am unable to share the "screenshot".
Firstly, you should use :
share.putExtra(Intent.EXTRA_STREAM. Uri.fromFile(new File(getApplicationContext().getFilesDir().getPath().toString() + "/screenshot.png"));
But you would then get "Permission denied for the attachment" and you possibly would try but with no luck :(
share.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
So probably the best way to deal with this issue is to store the captured screenshot in media library and send the file from there.
public void share() {
String filePath=getApplicationContext().getFilesDir().getPath().toString() + "/screenshot.png";
String path;
try {
path = Images.Media.insertImage(getContentResolver(), filePath, "title", null);
Uri screenshotUri = Uri.parse(path);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");
share.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
share.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(share, "Teilen via"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
Hope it would help!
maybe an easy question: I want to share a bitmap I received over the net to twitter/facebook/etc with the default share "intent".
The code I found
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("image/jpeg");
sendIntent.putExtra(Intent.EXTRA_STREAM, "IDONTKNOW");
sendIntent.putExtra(Intent.EXTRA_TEXT,
"See my captured picture - wow :)");
startActivity(Intent.createChooser(sendIntent, "share"));
needs to be filled at the point "IDONTKNOW" with the bitmap. (this.bitmap)
I found no way to handle this without saving the bitmap to internal sd..
regards
Simply, you can convert a bitmap into PNG from external storage.
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File imageFile = new File(path, getCurrentTime()+ ".png");
FileOutputStream fileOutPutStream = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 80, fileOutPutStream);
fileOutPutStream.flush();
fileOutPutStream.close();
Then, you can get a URI through Uri.parse:
return Uri.parse("file://" + imageFile.getAbsolutePath());
Might be a little late now, but you could also do String url = Images.Media.insertImage(context.getContentResolver(), image, "title", null); if you don't care how it's stored.
Ok I got it on my own, it seems there is no way to get the image uri without saving the bitmap to disk, therefore I use this simple method:
private Uri storeImage() {
this.storedImage = null;
this.storeImage = true;
// Wait for the image
while (this.storedImage == null && !this.stop)
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.storeImage = false;
FileOutputStream fileOutputStream = null;
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File file = new File(path, "cwth_" + getCurrentTime()+ ".jpg");
try {
fileOutputStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
this.storedImage.compress(CompressFormat.JPEG, JPEG_STORE_QUALITY, bos);
try {
bos.flush();
bos.close();
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return Uri.parse("file://" + file.getAbsolutePath());
}
Send Binary Content
Binary data is shared using the ACTION_SEND action combined with setting the appropriate MIME type and placing the URI to the data in an extra named EXTRA_STREAM. This is commonly used to share an image but can be used to share any type of binary content:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
source http://developer.android.com/training/sharing/send.html