How can i send either an image or a video file using ACTION_SEND? - android

I want to share either an image or a video file using ACTION_SEND. So basically when the users taps on an image and selects "share image/video" it should send either the image selected or the video selected.
here is my code i am using:
if (filep != null) {
}
File sending=new File(filep);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_SEND);
intent.setDataAndType(Uri.fromFile(sending),getMimeType(sending.getAbsolutePath()));
intent.putExtra(Intent.EXTRA_STREAM, sending);
startActivity(Intent.createChooser(intent , "Share"));
}
private String getMimeType(String url)
{
String parts[]=url.split("\\.");
String extension=parts[parts.length-1];
String type = null;
if (extension != null) {
MimeTypeMap mime = MimeTypeMap.getSingleton();
type = mime.getMimeTypeFromExtension(extension);
}
return type;
So when testing, it takes me to which app i want to use to share with i.e whatsapp, Facebook, email etc. And then when selecting either one, it then says "sharing failed, please try again." I can't seem to figure out why it doesn't work. However i have the same code to display either image or video file full screen with ACTION_VIEW and that seems to work great but not with sharing.
Can anyone assist please?

EXTRA_STREAM needs to be a Uri, and you are not passing a Uri. Use Uri.fromFile() to construct a Uri.
Also, replace setDataAndType() with setType(), as ACTION_SEND does not use the data aspect of the Intent.

Related

Android: Image resolution difference when sharing through Intent.EXTRA_STREAM

I have a menu option to share my app which generates an ArrayList of Uris object with a list of drawables Uris, and my issue is I've saved all the screenshots in 540x1170px and they get to email in 1620x3510px (curiously? exactly the triple), and in the mail they look too big.
This next is the code I use to share the images, and I'd like to know the reason why the original resolution is altered, and if there is a way to set intent image resolution.
public static void shareFile(ArrayList<Uri> uris, String fileType)
{
try{
Intent share = new Intent(Intent.ACTION_SEND_MULTIPLE);
share.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
share.setType(fileType);
String strSubject = TMLocale.getStringResourceByName("mainmenu_sharethisapp_subject");
share.putExtra(Intent.EXTRA_SUBJECT, strSubject);
share.putExtra(Intent.EXTRA_TEXT, TMLocale.getStringResourceByName("mainmenu_sharethisapp_recommendtext"));
share.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
activity.get().startActivity(Intent.createChooser(share, TMLocale.getStringResourceByName("mainmenu_sharethisapp_shareapptitle")));
}
catch(Exception ex){
Toast.makeText(activity.get(), String.valueOf(ex.getMessage()),Toast.LENGTH_LONG).show();
}
}
And I call it setting "image/jpg" as fileType.

I want to make android application to send picture, message to instagram users using instagram API

I've tried several sources from Google and GitHub but didn't find any authentic source that could help me sending multiple pictures and posts automatically from my android gallery using the scheduler. Is anyone working with the Instagram API? If so, could you please give me some authentic source?
Instead of using the Instagram API, you can directly start an Intent to post an Image or Video to Instagram by opening a "Share with" dialog. This will require user interaction.
String type = "image/*";
String filename = "/myPhoto.jpg";
String mediaPath = Environment.getExternalStorageDirectory() + filename;
private void createInstagramIntent(String type, String mediaPath){
// Create the new Intent using the 'Send' action.
Intent share = new Intent(Intent.ACTION_SEND);
// 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 to the Intent.
share.putExtra(Intent.EXTRA_STREAM, uri);
// Broadcast the Intent.
startActivity(Intent.createChooser(share, "Share to"));
}
Code example taken from https://www.instagram.com/developer/mobile-sharing/android-intents/

How to share text in Instagram

I need share text to instagram but I can't use android
intent.putExtra(Intent.EXTRA_TEXT,"MY TEXT");
nothing happen.please help me to do this
Instagram have stopped accepting pre-populated capitions to increase the quality of content in the system. See this post.
http://developers.instagram.com/post/125972775561/removing-pre-filled-captions-from-mobile-sharing
Generic code for sharing text with any social app :
Step1 : Get the package name of app you wanna share :
To get the package name use adb logcat -s ActivityManager this command in windows and run the app like for example you want package name for instagram so run above command and open instagram app you would get the package name in logs
Note : the adb command listed above is for windows .
For ubntu you can use adb logcat | grep "ActivityManager"
STEP 2 : Once you got the package name of app below is generic code for sharing text .
try {
Intent shareOnAppIntent = new Intent();
shareOnAppIntent .setAction(Intent.ACTION_SEND);
shareOnAppIntent .putExtra(Intent.EXTRA_TEXT, getResources().getString(R.string.share_body));
shareOnAppIntent .setType("text/plain");
shareOnAppIntent .setPackage(PACKAGE_NAME_OF_APP);
startActivity(shareOnAppIntent );
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(ShareAppActivity.this, "APP is not installed", Toast.LENGTH_LONG).show();
}
Unfortunately Instagram doesn't receives text from intent. it receives only EXTRA_STREAM object. you can share only images of format jpeg, gif, png. Since they are not providing any SDK you cant share in any other way.
Check Instagram developer documentation here they were clearly mentioning that the accepting Intent Parameter as EXTRA_STREAM
This is the code for sharing photo in Instagram
String type = "image/*";
String filename = "/myPhoto.jpg";
String mediaPath = Environment.getExternalStorageDirectory() + filename;
createInstagramIntent(type, mediaPath);
private void createInstagramIntent(String type, String mediaPath){
// Create the new Intent using the 'Send' action.
Intent share = new Intent(Intent.ACTION_SEND);
// 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 to the Intent.
share.putExtra(Intent.EXTRA_STREAM, uri);
// Broadcast the Intent.
startActivity(Intent.createChooser(share, "Share to"));
}
Here is the intent code to share image and text in Instagram.
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_STREAM,uri);
shareIntent.putExtra(Intent.EXTRA_TEXT,"YOUR TEXT TO SHARE IN INSTAGRAM");
shareIntent.setPackage("com.instagram.android");
return shareIntent;

Send File Path in order to choose the respective application to open in Android

In my application I will have list with different file paths.
Example list:
/storage/emulated/0/Edited/myjpeg_file.JPG
/storage/emulated/0/Downloads/bill.pdf
/storage/emulated/0/Downloads/mytextfile.txt
This list is showed in list view. When I click on one entry, it should open list of supported installed applications to process it further.
When I click on entry wit .JPG then it should show apps related to that.
And If i select .pdf file it should show supported apps
And for all paths default it should show file browser such as default file manager , Es Explorer etc...
I am referring to
http://developer.android.com/training/sharing/send.html
But need the selection based on file type (txt, xml, pdf jpeg etc..).
If i select .JPG file it is not showing gallery or Es file explorer (i have it on my mobile)
In same way when I click on .pdf file it is not showing adobe reader etc..
Below is my code:
public void openFile(String minmeType, Uri uriFromPath) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriFromPath);
if(minmeType.contains("text/plain")) {
shareIntent.setType("text/plain");
} else {
shareIntent.setType("image/jpeg");
}
startActivity(Intent.createChooser(shareIntent, "sending out"));
}
Above function is called from below code:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String path = pathList[position];
Uri uriFromPath = Uri.fromFile(new File(path));
Toast.makeText(getActivity().getApplicationContext(),"Uri: "+ uriFromPath, Toast.LENGTH_LONG).show();
if(path.contains(".JPG")) {
openFile("image/*", uriFromPath);
} else {
openFile("text/plain", uriFromPath);
}
return;
}
});
Have you tried using ACTION_VIEW instead of ACTION_SEND?
ACTION_SEND implies you want to send the document to somebody else. Adobe Reader won't catch those Intents that want to do ACTION_SEND, because sending pdfs is not what they do. VIEWing them, on the other hand, is.
You also need to match the intent that the target application is filtering for.
In this case, it's pretty simple what you want to do: you want Adobe Reader to open your file. And Adobe Reader actually has the intent filter you would expect. If you look into its Manifest file, you'll see that (among others) it includes an intent filter that matches Uris with schema file, any host, and any path that ends in .pdf. If you change your openFile method to this:
public void openFile(Uri uriFromPath) {
Intent shareIntent = new Intent(Intent.ACTION_VIEW, uriFromPath);
startActivity(Intent.createChooser(shareIntent, "sending out"));
}
It will work. It should also work for most apps that handle content from files.

How can I limit a user to only picking photos from Android's MediaStore?

I want users to be able to choose a photo from their media library but don't want them to be able to choose a video. I'm having trouble limiting them to only choosing a photo. When I open the gallery app using
new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
then they are able to choose both photos and videos. Is there a way to limit them to only choosing photos?
For that you need to also set a mime type to "image/*" using Intent.setType(String type).
UPDATE: Looks like the proper way to set Uri and mime type at once is:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
UPDATE 2: This is because when we set mime type or data uri separatelly look what happens (taken from Intent sources):
public Intent setType(String type) {
mData = null;
mType = type;
return this;
}
public Intent setData(Uri data) {
mData = data;
mType = null;
return this;
}
At least this is true on API 2.2.
Another option would be to use ACTION_GET_CONTENT instead of ACTION_PICK. It allows you to set a the MIME type you want without having to specify a URI at all.

Categories

Resources