sharing image with whatsapp in android - android

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

Related

Android Filter Intent Chooser

I would like to filter the list you see below. Only file-explorer should be choosable.
intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory() + "/Android/data/" + getContext().getPackageName() + "/Files");
intent.setDataAndType(uri, "*/*");
startActivity(Intent.createChooser(intent, getString(R.string.openFolder)));
EDIT:
This is not possible. There is no magic Intent that always only opens some magic app category.
First, anyone can write any app to respond to any desired implicit Intent.
Second, there is no universal definition of "file explorer". What you think a "file explorer" is may differ from what other developers think a "file explorer" is, which in turn may differ from what users think a "file explorer" is. A user's device may not even have a "file explorer", from anyone's definition.
Add this in your Intent, it would open a ES File Explorer
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(filePath)));
intent.setPackage("com.estrongs.android.pop");
startActivity(shareIntent);
You can call the below function
type is the package name of file-explorer
like if you want to share data to Twitter call it like initShareIntent("com.twitter.android");
private void initShareIntent(String type) {
boolean found = false;
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/jpeg");
// gets the list of intents that can be loaded.
List<ResolveInfo> resInfo = getActivity().getPackageManager().queryIntentActivities(share, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
if (info.activityInfo.packageName.toLowerCase().contains(type) ||
info.activityInfo.name.toLowerCase().contains(type)) {
//Share Data here
share.setPackage(info.activityInfo.packageName);
found = true;
break;
}
}
if (!found) {
Toast.makeText(getActivity(), share_type + " not found in Device", Toast.LENGTH_SHORT).show();
return;
}
startActivity(Intent.createChooser(share, "Select"));
}
}
It will only open the particular app to share data. Try this and let me know if it helped you
Make sure that the directory exists then try this :
Intent fileManagers = new Intent();
fileManagers.setAction(Intent.ACTION_GET_CONTENT);
File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
.getAbsolutePath());
Uri uri = Uri.fromFile(dir);
fileManagers.setDataAndType(uri, "file/*");
startActivity(Intent.createChooser(fileManagers, null));

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

packing my app and share to other + android

I'm creating an application for Android and I need a button to send my application to another phone.
I tried to put an apk and send to other but I can't do it.
I'm using this code :
Intent sharei=new Intent(Intent.ACTION_SEND);
sharei.putExtra(Intent.EXTRA_STREAM,Uri.parse("android.resource://com.packa.ge/raw/hafez.apk"));
sharei.setType("application/vnd.android.package-archive");
startActivity(Intent.createChooser(sharei, "share"));
but it isn't working.
I saw a Persian app do this: in a context menu one of the items was :"Send via Bluetooth" and when I touched this, it sent the apk file to the other phone.
I packed My app and put it to Raw folder to send, but this is not work correctly for 2nd or 3rd phone.
he said: "i create a application for android i need put button to send my application to other phone", i think he is talking about sending the same application he is running....
Andrea Bellitto
Yes. I need to send my running application.
resolved ...
try {
PackageManager pm = getPackageManager();
ApplicationInfo ai = pm.getApplicationInfo(getPackageName(), 0);
File srcFile = new File(ai.publicSourceDir);
Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
share.setType("application/vnd.android.package-archive");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(srcFile));
startActivity(Intent.createChooser(share, "PersianCoders"));
} catch (Exception e) {
Log.e("ShareApp", e.getMessage());
}
i find the code for changing base.apk to special file name ...
try {
PackageManager pm = getPackageManager();
ApplicationInfo ai = pm.getApplicationInfo(getPackageName(), 0);
File srcFile = new File(ai.publicSourceDir);
File outputFile = new File(Environment.getExternalStorageDirectory(),
"hamed-heydari_Com" + ".apk");
Tools.copy(srcFile, outputFile);
Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_TEXT, Tools.getStringByName("installApp") + " " + Tools.getStringByName("app_name"));
share.setType("application/vnd.android.package-archive");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(outputFile));
startActivity(Intent.createChooser(share, "Share App ..."));
} catch (Exception e) {
Log.e("ShareApp", e.getMessage());
}

photo share intent in android

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.

Categories

Resources