Android - Intent Sharing - java

How to detect from which (any) external app(or packagname) data was shared:
Via sharing an image/data (intent) from another app to my app(activity),

private void shareImage() {
Intent share = new Intent(Intent.ACTION_SEND);
// If you want to share a png image only, you can do:
// setType("image/png"); OR for jpeg: setType("image/jpeg");
share.setType("image/*");
// Make sure you put example png image named myImage.png in your
// directory
String imagePath = Environment.getExternalStorageDirectory()
+ "/myImage.png";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image!"));
}

Related

How can I send audio files from my app to Whatsapp?

I'm working on an app from which you can send audio to whatsapp. However, the codes that I am using are not working.
//This code returns an error and asks to try again to send the audio file
String sharePath = Environment.getExternalStorageDirectory().getPath()
+ "android.resource://"
+ wspActivity.this.getPackageName() + "/raw/" + "increible";
Uri uri = Uri.parse(sharePath);
Intent share = new Intent(Intent.ACTION_SEND);
share.setPackage("com.whatsapp");
share.setType("audio/*");
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Sound File"));
//This code indicates that the file is of an incompatible type.
final Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/mp3");
final String path = copyFiletoExternalStorage(R.raw.increible, "increible.mp3");
final Uri soundUri = Uri.parse(path);
share.putExtra(Intent.EXTRA_STREAM, soundUri);
startActivity(Intent.createChooser(share, "Compartiendo..."));
If anyone has any idea why this behaviour, I would appreciate it if you could share it :)
I need to improve some of these codes to be able to send audio files from my app through whatsapp

Image sharing to the other application by Intent

I am making an app and I wanna share images from my app to a specific app
so i used this code to share via choose
try {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
// path of image
final String imageURIPath = "image-path";
Uri imageURI = Uri.parse(imageURIPath);
sharingIntent.setType("image/jpeg");
sharingIntent.setType("image/jpg");
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_STREAM, imageURI);
startActivity(Intent.createChooser(sharingIntent, "Share via app"));
} catch (Exception e) {
showMessage(e.toString());
}
and all work perfectly but I need to share to a specific app like this
shareIntent = getPackageManager().getLaunchIntentForPackage("app_PackageName");
startActivity(shareIntent);
so 'app_PackageName' chage to like app what i need like this example
shareIntent = getPackageManager().getLaunchIntentForPackage("com.adobe.lrmobile");
startActivity(shareIntent);
and I build the app without errors but when I wanna share to that app it just opens that app

GIF is getting shared as an image not animated - Android Java

I am trying to share an GIF programmatically, but it is getting shared as an image (not animated)
Code Snippet:
Intent shareIntent = new Intent();
shareIntent.setType("image/gif");//image/gif
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(myList.get(item)));
startActivity(Intent.createChooser(shareIntent,"Share gif"));
You are trying to share to which apps?
Some whatsapp builds don't support gif feature.
You can check this question here with some additional information: How to share GIF image in Whatsapp programmatically in Android?
There is this comment:
"This solution doesn't work with whatsapp but does work with all the other apps I specified above (namely Hangouts, Android Messages, Facebook Messenger)"
He is referring to this answer:
private void ShareGif() {
// replace the path and file name with your gif image path and name
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "temporary_file.gif";
File sharingGifFile = new File(baseDir, fileName);
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/gif");
Uri uri = Uri.fromFile(sharingGifFile);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share image"));
}
/*
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setPackage("com.whatsapp");
shareIntent.putExtra(Intent.EXTRA_TEXT,title + "\n\nLink : " + link );
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(sharePath));
shareIntent.setType("image/*");
startActivity(shareIntent);
*/
Hope it helps.

Save drawables in local storage as cache and then read them for a Share_Intent?

I have drawables in my project that i have declared as seperate resource array
public Integer[] mThumbIds = {
R.drawable.ic_bl1,
R.drawable.ic_bl2,
R.drawable.ic_bl3,
R.drawable.ic_bl4,
R.drawable.ic_bl5,
R.drawable.ic_ca1,
R.drawable.ic_ca2,
R.drawable.ic_ch}
I want to save them all in a temp folder on local storage of android(not SD Card)so that I can retrieve them later and send them via a share intent.I have code for share Intent completed
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/*");
sharingIntent.putExtra(Intent.EXTRA_STREAM, path);
// sharingIntent.setPackage("com.facebook.orca");
sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent chooserIntent = Intent.createChooser(sharingIntent, "Send Via");
chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sContext.startActivity(chooserIntent);
// sContext.startActivity(sharingIntent);
}
});
I need to pass a Uri of a particular file to be sent,and best way to get it is through saving images in local storage and then retrieving there Uri.I know how to save a single image in cache and then retrieving it.
I can't figure out how to save a list of drawables in a particular folder on local storage and then retrieving there Uris for a use in share intent later.
You could use this code.
But add read and write storage permission.
public void onClick(View view) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/png");
ImageView imageView = (ImageView) view;
Drawable mDrawable = imageView.getDrawable();
Bitmap mBitmap = ((BitmapDrawable) mDrawable).getBitmap();
String path = MediaStore.Images.Media.insertImage(mContext.getContentResolver(), mBitmap, "Image Description", null);
Uri uri = Uri.parse(path);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share Content!!"));
}

Android: Share Image intent not working with Facebook?

Hi I have the following code to share an image:
// Share
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
Uri uri = Uri.parse(getFilesDir() + File.separator + "myGoal.jpg");
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image"));
It works to share the image to Dropbox but if I pick the Facebook option, I get Facebook's status update dialog with no image attached and if I try to update my status with 'Test' it doesn't work. No errors. Just not working.
I know it's not the image because it uploads to my Dropbox properly and I can pull up the image and look at it.
Do I have to attach the image to the intent differently for it to work with Facebook?
Any ideas? I'm debugging on a physical device.
So I figured out the problem.
I was saving the picture to internal storage with getFilesDir() which put the picture into my apps sandbox and made inaccessible to the other apps.
I replaced my code with the following:
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/MyApp/";
File dir = new File(file_path);
dir.mkdirs();
File file = new File(dir, "myPic.png");
FileOutputStream fOut = new FileOutputStream(file);
screenshot.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
// Share
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
share.putExtra(Intent.EXTRA_TEXT, "My Image");
startActivity(Intent.createChooser(share, "Share Image"));
Works perfectly fine now.
What i have done to post image on facebook is instead of passing it directly i create a function and write it like this :
private void ShareWall(String message) {
Bundle parameters = new Bundle();
// share msg
parameters.putString("message", message);
// shre image which you have define above
parameters.putString("picture", postImage);
try {
facebook.request("me");
String response = facebook.request("me/feed", parameters, "POST");
Log.d("response: ", response);
if (response == null || response.equals("")) {
response.equals("");
showToast("No Response.");
} else {
showToast("Message has been posted to your walll!.");
}
finish();
} catch (Exception e) {
showToast("Message failed to posdt on wall.");
e.printStackTrace();
finish();
}
}
Hope this help you.
You can still share images (but not text) from your app to Facebook even if you are not using the Facebook SDK.
Just make sure that you use Uri.fromFile instead of Uri.parse and it will work:
DO NOT USE:
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(pathToFile));
USE:
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(pathToFile)));
Here is a solution without using external file write:
Drawable mDrawable = myImageView1.getDrawable();
Bitmap mBitmap = ((BitmapDrawable)mDrawable).getBitmap();
String path = MediaStore.Images.Media.insertImage(getContentResolver(), mBitmap, "Image I want to share", null);
Uri uri = Uri.parse(path);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share Image"));
In this case, my image comes from an ImageView myImageView.

Categories