- Sharing Content with Intents
- Share application «link» in Android
- 13 Answers 13
- android share image from url
- 10 Answers 10
- How to use «Share image using» sharing Intent to share images in android?
- 18 Answers 18
- Android Share Intent Example for URL, Text or Image
- Android Share Intent Example Step by Step
- Step 1: Prepare XML Layout, we’ll have activity_main.xml
- Step 2: Inside the onCreate method of MainActivity.java, put the buttons and OnClickListener handlers.
- Step 3: As you’ve noticed, we have shareTextUrl() method that will be triggered every time the user clicks on the “Share Text or URL” button.
- Step 4: We also have shareImage() method for sharing images. It is triggered when the user clicks on the “Share Image” button.
- Complete MainActivity.java code:
- Today’s code output screenshots
Sharing Content with Intents
Intents allow us to communicate data between Android apps and implicit intents can also accept actions. One of those actions is the ACTION_SEND command which indicates we want to send data across apps. To send data, all you need to do is specify the data and its type, and the system will identify compatible receiving activities and display them to the user.
Sending and receiving data between applications with intents is most commonly used for social sharing of content. Intents allow users to share information quickly and easily, using their favorite applications.
You can send content by invoking an implicit intent with ACTION_SEND .
To send images or binary data:
Sending URL links should simply use text/plain type:
In certain cases, we might want to send an image along with text. This can be done with:
Sharing multiple images can be done with:
See this stackoverflow post for more details.
Note: Facebook does not properly recognize multiple shared elements. See this facebook specific bug for more details and share using their SDK.
Facebook doesn’t work well with normal sharing intents when sharing multiple content elements as discussed in this bug. To share posts with facebook, we need to:
- Create a new Facebook app here (follow the instructions)
- Add the Facebook SDK to your Android project
- Share using this code snippet:
You may want to send an image that were loaded from a remote URL. Assuming you are using a third party library like Glide, here is how you might share an image that came from the network and was loaded into an ImageView. There are two ways to accomplish this. The first way, shown below, takes the bitmap from the view and loads it into a file.
and then later assuming after the image has completed loading, this is how you can trigger a share:
Make sure to setup the «SD Card» within the emulator device settings:
Note that if you are using API 24 or above, see the section below on using a FileProvider to work around new file restrictions.
If you are targeting Android API 24 or higher, private File URI resources (file:///) cannot be shared. You must instead wrap the File object as a content provider (content://) using the FileProvider class.
First, you must declare this FileProvider in your AndroidManifest.xml file within the tag:
Next, create a resource directory called xml and create a fileprovider.xml . Assuming you wish to grant access to the application’s specific external storage directory, which requires requesting no additional permissions, you can declare this line as follows:
Finally, you will convert the File object into a content provider using the FileProvider class:
If you see a INSTALL_FAILED_CONFLICTING_PROVIDER error when attempting to run the app, change the string com.codepath.fileprovider in your Java and XML files to something more unique, such as com.codepath.fileprovider.YOUR_APP_NAME_HERE .
Note that there are other XML tags you can use in the fileprovider.xml , which map to the File directory specified. In the example above, we use Context.getExternalFilesDir(Environment.DIRECTORY_PICTURES) , which corresponded to the XML tag in the declaration with the Pictures path explicitly specified. Here are all the options you can use too:
XML tag | Corresponding storage call | When to use |
---|---|---|
Context.getFilesDir() | data can only be viewed by app, deleted when uninstalled ( /data/data/[packagename]/files ) | |
Context.getExternalFilesDir() | data can be read/write by the app, any apps granted with READ_STORAGE permission can read too, deleted when uninstalled ( /Android/data/[packagename]/files ) | |
Context.getCacheDir() | temporary file storage | |
Environment.getExternalStoragePublicDirectory() | data can be read/write by the app, any apps can view, files not deleted when uninstalled | |
Context.getExternalCacheDir() | temporary file storage with usually larger space |
If you are using API 23 or above, then you’ll need to request runtime permissions for Manifest.permission.READ_EXTERNAL_STORAGE and Manifest.permission.WRITE_EXTERNAL_STORAGE in order to share the image as shown above since newer versions require explicit permisions at runtime for accessing external storage.
Note: There is a common bug on emulators that will cause MediaStore.Images.Media.insertImage to fail with E/MediaStore﹕ Failed to insert image unless the media directory is first initialized as described in the link.
This is how you can easily use an ActionBar share icon to activate a ShareIntent. The below focuses on the support ShareActionProvider for use with AppCompatActivity .
Note: This is an alternative to using a sharing intent as described in the previous section. You either can use a sharing intent or the provider as described below.
First, we need to add an ActionBar menu item in res/menu/ in the XML specifying the ShareActionProvider class.
Next, get access to share provider menu item in the Activity so we can attach the share intent later:
Note: ShareActionProvider does not respond to onOptionsItemSelected() events, so you set the share action provider as soon as it is possible.
Now, once you’ve setup the ShareActionProvider menu item, construct and attach the share intent for the provider but only after image has been loaded as shown below using the RequestListener for Glide .
Note: Be sure to call attachShareIntentAction method both inside onCreateOptionsMenu AND inside the onResourceReady for Glide to ensure that the share attaches properly.
We can use a similar approach if we wish to create a share action for the current URL that is being loaded in a WebView:
Check out the official guide for easy sharing for more information.
Источник
Share application «link» in Android
I want my application user to be able to share/recommend my app to other users. I use the ACTION_SEND intent. I add plain text saying something along the lines of: install this cool application. But I can’t find a way to enable users to directly go to the install screen of market place for instance. All I can provide them with is a web link or some text. In other words I am looking for a very direct way for android users to have my app installed.
Thanks for any help/pointers,
13 Answers 13
This will let you choose from email, whatsapp or whatever.
You can use also ShareCompat class from support library.
You would want to provide your users with a market:// link which will bring them directly to the details page of your app. The following is from developer.android.com:
Loading an application’s Details page
In Android Market, every application has a Details page that provides an overview of the application for users. For example, the page includes a short description of the app and screen shots of it in use, if supplied by the developer, as well as feedback from users and information about the developer. The Details page also includes an «Install» button that lets the user trigger the download/purchase of the application.
If you want to refer the user to a specific application, your application can take the user directly to the application’s Details page. To do so, your application sends an ACTION_VIEW Intent that includes a URI and query parameter in this format:
In this case, the packagename parameter is target application’s fully qualified package name, as declared in the package attribute of the manifest element in the application’s manifest file. For example:
Источник
android share image from url
I want to share an image using the code:
I made a button that call the code above. The share intent open but I got if I click on «Share by MMS»: «cannot add this picture to your message». If Facebook I got only a text area without my picture.
10 Answers 10
An adapted version of @eclass’s answer which doesn’t require use of an ImageView:
Use Picasso to load the url into a Bitmap
Convert Bitmap into Uri
I use these codes from this tutorial
then add this to your activity
then add your application manifest
You need to use a local file. Like this:
If your image is on remote server download it to the device first.
After a lot of pondering, here is the code that I found to be working for me! I think this is one of the simplest versions of code to achieve the given task using Picasso. There is no need to create an ImageView object.
Here, bmp is a class level Bitmap variable and url will be the dynamic Internet url to your image for sharing. Also, instead of keeping the code to share inside the onBitmapLoaded() function, it can also be kept inside another handler function and later called from the onBitmapLoaded() function. Hope this helps!
First you need to load image in the glide. Then you can share it to anywhere. Code to load image from glide (image is being saved to storage, you can delete it later).
Источник
How to use «Share image using» sharing Intent to share images in android?
I have image galley app in that app I placed all the images into the drawable-hdpi folder. and i called images in my activity like this :
So now i want know how do i share this images using sharing Intent i putted sharing code like this :
And i have sharing button also when i click on share button Sharing box is opening But when i cliked any service mostly its crashing or some services say : unable to open image So how i can fix this or is there any othere format code to share images .
Edit :
I tried using the code below. But its not working.
If don’t mind somebody plz correct my above code OR give me a proper example plz How do i Share my images from drawable-hdpi folder
18 Answers 18
The solution proposed from superM worked for me for a long time, but lately I tested it on 4.2 (HTC One) and it stopped working there. I am aware that this is a workaround, but it was the only one which worked for me with all devices and versions.
According to the documentation, developers are asked to «use the system MediaStore» to send binary content. This, however, has the (dis-)advantage, that the media content will be saved permanently on the device.
If this is an option for you, you might want to grant permission WRITE_EXTERNAL_STORAGE and use the system-wide MediaStore.
First add permission
using Bitmap from resources
Tested via bluetooth and other messengers
I found the easiest way to do this is by using the MediaStore to temporarily store the image that you want to share:
Simple and Easiest code you can use it to share image from gallery.
How to share image in android progamatically , Sometimes you wants to take a snapshot of your view and then like to share it so follow these steps: 1.Add permission to mainfest file
2.Very First take a screenshot of your view , for example it is Imageview, Textview, Framelayout,LinearLayout etc
For example you have an image view to take screenshot call this method in oncreate()
after taking screenshot call share image method either on button
click or where you wants
After on create method define these two method ##
Here is a solution that worked for me. One gotcha is you need to store the images in a shared or non app private location (http://developer.android.com/guide/topics/data/data-storage.html#InternalCache)
Many suggestions say to store in the Apps «private» cache location but this of course is not accessable via other external applications, including the generic Share File intent which is being utilised. When you try this, it will run but for example dropbox will tell you the file is no longer available.
/* STEP 1 — Save bitmap file locally using file save function below. */
/* STEP 2 — Share the non private Absolute file path to the share file intent */
/* SAVE IMAGE FUNCTION */
/* STEP 3 — Handle Share File Intent result. Need to remote temporary file etc. */
Источник
Android Share Intent Example for URL, Text or Image
Today I’m going to give you an android share intent example that you can use to enable your app to share contents such as URL or text and Image to other apps installed in your Android device like Facebook, Twitter, Messaging, Instagram, Evernote, etc.. Example uses of this code include:
You are building an app that browses a certain website or URL.
Your app generates an image that a user can share.
Android Share Intent Example Step by Step
Step 1: Prepare XML Layout, we’ll have activity_main.xml
Step 2: Inside the onCreate method of MainActivity.java, put the buttons and OnClickListener handlers.
Step 3: As you’ve noticed, we have shareTextUrl() method that will be triggered every time the user clicks on the “Share Text or URL” button.
Step 4: We also have shareImage() method for sharing images. It is triggered when the user clicks on the “Share Image” button.
Complete MainActivity.java code:
Please note that when you use the setType() method, you are enabling Android to filter what apps can share your content. For example, you are sharing a text or URL, the appropriate apps to be shown can be Facebook, Messaging or Email. If you are sharing an image, proper apps can be Instagram, Snapseed or Picasa.
Today’s code output screenshots
Share Text or URL button was touched. Take note of the apps on the list.
User chose to share to Facebook
“Share Image” button was touched. Apps was different on the list.
User chose to share with Gmail. Our image was attached.
User chose to share to Snapseed.
If you have any suggestions to improve this post, please drop it in the comments section below! I’ll be glad to update this post for further improvement. Thanks for reading our Android Share Intent Example!
Источник