how to save and share bitmap in android - android

I have activity with image view and share button,
the image retrieved from DB
I want to save the image at first then share it
every things is going fine , the image saved in internal memory and it's going to share but when the other app runs (like whatsapp or messaging) it says file not support
I have push other image to ddms manually and then share is works without any problem !! O.o
I think the problem is from saving the bitmap, even I checked the saved bitmap from the ddms and it was looks good
there is my codes:
save
try {
FileOutputStream out = new FileOutputStream(new File(getFilesDir(), "temp.png"));
imageview.setImageBitmap(b1);
b1.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
}
catch (Exception e) {}
share method
private void initShareIntent(String type,String _text)
{
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(new File(getFilesDir(), "temp.png")));
shareIntent.setType("image/PNG");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "send"));
}

Edit this
shareIntent.setType("image/*");
To this
shareIntent.setType("image/png");
I think it should work. Let me know if it works for you!!
[EDIT1] changed to png (instead of PNG) cause I think the case matters here. lol
[EDIT2] for multiple images try this (provided by GOOGLE :P)
ArrayList<Uri> imageUris = new ArrayList<Uri>();
imageUris.add(imageUri1); // Add your image URIs here
imageUris.add(imageUri2);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share images to.."));

Related

Instagram sharing issue: Always I get the message “Unable to load image”

I'm working on share functionality
I 've a problem with Instagram Share
Here is my code :
I'm trying share "account" image which is stored in "drawable" folder.This is just an example
try {
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.parse("android.resource://com.example.swathi.booklender/drawable/account");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setPackage("com.instagram.android");
startActivity(shareIntent);
}catch (Exception e)
{
Toast.makeText(getActivity(),"No Activity available, install instagram",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
How to I get rid of this problem?
You can't share drawable directly
First create a file from drawable then share it
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpg");
File image = new File(getFilesDir(), "foo.jpg");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(image));
startActivity(Intent.createChooser(shareIntent, "Share image via"));

How to use bitmap to send to another app

can someone help me how to send an image to another application in android?
I want to send an image to be used as wallpaper, BBM display picture, and other applications that can use it (like WhatsApp, contacts, etc.)
I use this code, but it can only be used for sending text and not images as I want
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);
Then I tried to use this code to set the wallpaper
public void setAsWallpaper(Bitmap bitmap) {
try {
WallpaperManager wm = WallpaperManager.getInstance(_context);
wm.setBitmap(bitmap);
//disinimas
Toast.makeText(_context,
_context.getString(R.string.toast_wallpaper_set),
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(_context,
_context.getString(R.string.toast_wallpaper_set_failed),
Toast.LENGTH_SHORT).show();
}
}
the problem with the code, bitmap images directly applied as wallpaper. I wanted like sending text above, the user can choose to use another application. So I want a bitmap image that can later be used for wallpaper, BBM display picture, or other applications that support it
bitmap variable already contains the image that I want to, the images obtained from the internet with this code:
Bitmap bitmap = ((BitmapDrawable) fullImageView.getDrawable())
.getBitmap();
I use this code and its work, but give me a message BBM: File not found, WhatsApp: File is not an image:
Bitmap icon = bitmap;
Intent share = new Intent(Intent.ACTION_ATTACH_DATA);
share.setType("image/jpeg");
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, "title");
values.put(Images.Media.MIME_TYPE, "image/jpeg");
Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI,
values);
OutputStream outstream;
try {
outstream = getContentResolver().openOutputStream(uri);
icon.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
outstream.close();
} catch (Exception e) {
System.err.println(e.toString());
}
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image"));
Thanks for your help Antrromet, finally I solved my problem with the following code:
Bitmap icon = bitmap;
// Intent share = new Intent(Intent.ACTION_SEND);
// share.setType("image/*");
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, "title");
values.put(Images.Media.MIME_TYPE, "image/*");
Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI,
values);
OutputStream outstream;
try {
outstream = getContentResolver().openOutputStream(uri);
icon.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
outstream.close();
} catch (Exception e) {
System.err.println(e.toString());
}
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setDataAndType(uri, "image/*");
intent.putExtra("mimeType", "image/*");
this.startActivity(Intent.createChooser(intent, "Set as:"));
Now the image can be used as Wallpaper, BBM profile picture, WA display Picture, Coontact display picture, etc. Thanks
Did you try using the following code? It is use to send binary data to other apps.
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)));
Or if you dont have the URI with you but have the Bitmap instead, then try using the code as given here.
UPDATE:
Setting wallpaper and profile picture for BBM are totally different things and there is no common intent for them. For setting the wallpaper, you can try the following as given here.
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setDataAndType(uri, "image/jpeg");
intent.putExtra("mimeType", "image/jpeg");
this.startActivity(Intent.createChooser(intent, "Set as:"));
For BBM, the APIs are not open for changing the user image. So you cannot do that through your app.

Sharing data on Android with Intent

I'm having a problem with sharing content on android between apps, with Intent
I can successfully share plain text (on G+, Facebook and Facebook Messenger)
The code is the following:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT,"Your score is: "+Integer.toString(mScore));
shareIntent.setType("text/plain");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
But when it comes to images things don't work.
Intent is created and I can actually choose between the apps to share the image with, but when the selected app tries to start, it just crashes and doesn't open.
No app works (tested with 9Gag, G+, Facebook, Facebook Messenger, Photos..)
The code I'm using is this:
Intent shareIntent = new Intent();
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/png");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
The image is stored on
/data/data/com.test.carloalberto.myapp/files/score.png
which is the right path where i previously saved the image.
Why do other apps crash when trying to share an image?
Thanks for reply.
EDIT: I stored the image in the public picture directory, but apps like fb messenger trying to share it still crash!
This is the new code:
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String name = "score.jpg";
File file = new File(dir,name);
FileOutputStream ostream;
try {
if(!dir.exists()) {
Log.i("Share: onClick()","Creating directory "+dir);
dir.mkdirs();
}
Log.i("Share: onClick()","Final path: "+file.getAbsolutePath());
file.createNewFile();
ostream = new FileOutputStream(file);
Log.i("onClick(): Share action: ","Path: "+file.getAbsolutePath());
bitmap.compress(Bitmap.CompressFormat.JPEG,100,ostream);
ostream.close();
Uri uriToImage = Uri.parse(file.getAbsolutePath());
Log.i("onClick(): Share action, final uri ",uriToImage.toString());
Intent shareIntent = new Intent();
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
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)));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
Now the file path is: /mnt/sdcard/Pictures/score.jpg, which should be public and accessible by all other applications.
(Obviously I also set the permissions in the manifest file)
/data/* path is not available to apps (security model of the filesystem every *nix user should be aware of).
The fact you are able to access this folder on your rooted device does not mean the app could do the same.
You could either store your image in publicly available directory (such as Pictures folder of the device, etc), or try to construct URI for your asset using the scheme:
android.resource://[package]/[res type]/[res name]
Try this.
private void shareIntent(String type,String text){
File filePath = getFileStreamPath("shareimage.jpg");
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(new File(filePath)));
shareIntent.setType("image/*"); //"image/*" or "image/jpeg", etc
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "send"));
}

Android Application with Intents

I'm trying to do an Intent to share an image from an SD card. But I have a problem with the share button: It says:
no app can perform this action
So, which type of code I have to use to share an image? I want, maybe, to have the possibility to send my image as an e-mail or a message. Hope someone can help me.
first you need to store img in storege
like this way
Bitmap icon = mBitmap;
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
and then you can share it like this way
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg"));
startActivity(Intent.createChooser(share, "Share Image"));
"no app can perform this action" because, there was no sharing application installed in your device. Install some third party applications like facebook, whats app, gmail... in your real device and run your application to share image.
and also start activity like below:
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg"));
startActivity(Intent.createChooser(share, "Share Image"));

share picture via Intent (Facebook and co)

I'm wasting a lot of time, trying to get sharing a simple jpeg image working via sharing Intent.
The most important one as usual is Facebook, and as usual it doesn't work.
Intent shareIntent = new Intent(Intent.ACTION_SEND);
Uri picUri = Uri.parse("http://someserver.com/somepic.jpg");
shareIntent.setType("image/jpeg");
shareIntent.putExtra(Intent.EXTRA_STREAM, picUri);
shareIntent.putExtra(Intent.EXTRA_TEXT, someString);
startActivity(Intent.createChooser(shareIntent, "some text..."));
The chooser opens nicely, Facebook also opens and makes me login, but then it tells me that updoad failed.
I also tried Flicker and Mail, and they all fail.
Then I tried to write the image to local file and send from there, also failed:
Drawable dr = ImageLoader.getDrawable(url);
Bitmap bmp = ((BitmapDrawable)dr).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 80, stream);
byte[] data = stream.toByteArray();
FileOutputStream ostream;
try {
ostream = this.openFileOutput("pic.jpeg", Context.MODE_WORLD_READABLE);
ostream.write(data);
} catch (Exception e) {
e.printStackTrace();
}
Uri picUri = Uri.fromFile(new File("pic.jpeg"));
shareIntent.putExtra(Intent.EXTRA_STREAM, picUri);
I've no clue if I do that correctly, haven't done that before.
My last try was to just send an HTML string with the image included as img tag. But Facebook seems not to handle type "text/html", so it's not an option.
I'm sure it needs only few lines of code. But which ones?
edit
I forgot the line
shareIntent.putExtra(Intent.EXTRA_STREAM, picUri);
in first code snippet. It was there when I tried, didn't work either.
No sleep for too long...
I think this will answer your question. https://stackoverflow.com/a/3553102/478537
It looks like you are on the right path, all I can see is that in your first code snippet, you are not using the picUri anywhere, and therefore its not being sent, and in the second, you are setting the EXTRA_STREAM twice (which wouldn't cause any issues, just redundant code).
..Intent.EXTRA_TEXT, someString.
This will fail your transaction with Facebook- not "someString"- Facebook sharing expects and searches for an URL transmitted over the EXTRA_TEXT. Why - don't ask..I did not understand the guys
Well, I spent a lot of time, and the issue was the extension of file (png made a problem so ignore the file extension in this case and use "jpg"), try the following code
public static void shareImageFileWithIntent(File imageFile, Context context) {
MimeTypeMap mime = MimeTypeMap.getSingleton();
String type = mime.getMimeTypeFromExtension("jpg");
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType(type);
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
sharingIntent.putExtra(Intent.EXTRA_TEXT, "your extra text");
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "your subject");
context.startActivity(Intent.createChooser(sharingIntent, context.getString(R.string.share_image_intent)));
}
Instead of passing image Via intent, you can create a new Class and save it there from one activity. And access that image from another activity.

Categories

Resources