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"));
Related
I want to capture and share a view as an image.
My code runs without errors. The sharing part works well, but the capture does not work.
Any help is welcome.
Here the code i use.
linel1.buildDrawingCache();
Bitmap captureView = linel1.getDrawingCache();
FileOutputStream fos;
try {
fos = new FileOutputStream(Environment.getExternalStorageDirectory().toString()+"/DCIM/capture.jpeg");
captureView.compress(Bitmap.CompressFormat.JPEG, 100, fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Captured!", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_SUBJECT, "title");
Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory().toString()+"/DCIM/capture.jpeg"));
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "This picture is shared"));
I think you forget linel1.setDrawingCacheEnabled(true) before linel1.buildDrawingCache();
You can read more about drawing cache in this answer on SO.
I have image in assets folder and need to share it with whatsapp application
I tried this code , it keeps give me sharing failed try again ! what's wrong ?!
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.setPackage("com.whatsapp");
// share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File("file:///assets/epic/adv.png")));
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///assets/epic/adv.png"));
this.startActivity(Intent.createChooser(share, "share_via"));
This code for share image via whatsapp worked fine for me .
public void shareImageWhatsApp() {
Bitmap adv = BitmapFactory.decodeResource(getResources(), R.drawable.adv);
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.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.jpg"));
if(isPackageInstalled("com.whatsapp",this)){
share.setPackage("com.whatsapp");
startActivity(Intent.createChooser(share, "Share Image"));
}else{
Toast.makeText(getApplicationContext(), "Please Install Whatsapp", Toast.LENGTH_LONG).show();
}
}
private boolean isPackageInstalled(String packagename, Context context) {
PackageManager pm = context.getPackageManager();
try {
pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
return true;
} catch (NameNotFoundException e) {
return false;
}
}
You have several problems.
First, file:///assets/ is not a valid Uri on any version of Android. Your own application can refer to its own assets via file:///android_asset/.
Second, only you can access your own assets via file:///android_asset/ -- you cannot pass such a Uri to third-party apps. Either copy the file from assets into internal storage and use FileProvider, or you can try my StreamProvider and try to share the data straight out of assets/.
Third, there is no guarantee that com.whatsapp exists on the device, or that com.whatsapp will support ACTION_SEND of file:/// Uri values with a MIME type of image/*, and so you may crash with an ActivityNotFoundException.
Fourth, the user might want to share this image via some other means than WhatsApp. Please allow the user to share where the user wants, by removing the setPackage() call from your Intent.
This worked for me
Bitmap imgBitmap=BitmapFactory.decodeResource(getResources(),R.drawable.image);
String imgBitmapPath= MediaStore.Images.Media.insertImage(getContentResolver(),imgBitmap,"title",null);
Uri imgBitmapUri=Uri.parse(imgBitmapPath);
Intent shareIntent=new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM,imgBitmapUri);
startActivity(Intent.createChooser(shareIntent,"share image using"));
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.."));
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setType("image/*");
Uri uri = Uri.parse(pathToImage);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
return shareIntent;
i was used above code for sharing image on social sites.when i posting image on facebook only text is posted and image is not coming.how can we get the image and pathtoimage is string variable i am getting sever image path and stored in string variable.but it is not working.please give any solutions for my above question.?
Try this
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();
}
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg"));
startActivity(Intent.createChooser(share, "Share Image"));
Refer more links
http://android-developers.blogspot.in/2012/02/share-with-intents.html
Android Share Via Dialog
Image post into facebook wall from android sdcard
This is the solution I came up with:
This function allows you to share to instagram, facebook or any other app if you know the package name. If the app is not installed it redirects to the Play Store. If you send "others" as parameters, you will display a list of apps that also support image sharing.
shareImageIntent(mActivity,"image/*", "/storage/emulated/0/Pictures/Instagram/IMG_20150120_095603.jpg", "Instagram Sharing test. #Josh","com.instagram.android"); // parameters - for facebook: "com.facebook.katana" , to display list of other apps you can share to: "others"
public void shareImageIntent(Activity a,String type, String mediaPath, String caption, String app){
Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(app);
if ((intent != null)||(app.equals("others"))){
// Create the new Intent using the 'Send' action.
Intent share = new Intent(Intent.ACTION_SEND);
if(!app.equals("others"))
share.setPackage(app);
// Set the MIME type
share.setType(type);
// Create the URI from the media
File media = new File(mediaPath);
Uri uri = Uri.fromFile(media);
// Add the URI and the caption to the Intent.
share.putExtra(Intent.EXTRA_STREAM, uri);
share.putExtra(Intent.EXTRA_TEXT, caption);
// Broadcast the Intent.
//startActivity(Intent.createChooser(share, "Share to"));
a.startActivity(share);
}
else{
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//intent.setData(Uri.parse("market://details?id="+"com.instagram.android"));
intent.setData(Uri.parse("market://details?id="+app));
a.startActivity(intent);
}
}
You can not use Intent to post to Facebook. They have specifically blocked this. You have to either use their SDK or copy it to the clipboard and have the user paste it after the facebook interface opens. I am having this same problem.
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.