Share image with ShareActionProvider from Universal Image Loader - android

I'm using UIL to display images in a gridview. When one is selected it displays in a fullscreen window (UIL's ImagePagerActivity class) This screen allows you to flip through the available pictures. I want to use the ShareActionProvider in the action bar to share the currently displayed picture. I keep getting a null pointer exception when loading the onCreateOptionsMenu where it sets the shareIntent. I think it's not finding the displayed picture. Can someone look over this and help?
XML
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_item_share"
android:actionProviderClass="android.suport.v7.widget.ShareActionProvider"
android:showAsAction="ifRoom"
android:title="Share"/>
</menu>
Android
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.album_menu, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
mShareActionProvider.setShareIntent(getDefaultIntent());
return true;
}
private Intent getDefaultIntent() {
Bitmap cachedImage = imageLoader.getMemoryCache().get(imageUrls[pagerPosition]);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, cachedImage);
return intent;
}

Related

Android: SharingActionView on swipe from bottom

I would like to share data with other Apps, but I don't want the standard popup which appears bottom tool bar; I want this kind of view:
I followed the official document: https://developer.android.com/training/sharing/shareaction.html
So is there a native component to do that in Android SDK? Or is there any library to do that?
thank for your help guys!
You can do it like this :
First create a menu item :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.rashish.myapplication.MainActivity">
<item
android:id="#+id/action_share"
android:orderInCategory="100"
android:title="#string/action_settings"
android:icon="#android:drawable/ic_menu_share"
app:showAsAction="always" />
</menu>
Then create a method to show the Intent chooser dialog, like this :
private void share() {
String textToShare = "hello";
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
// set the type here i.e, text/image/ etc.
sharingIntent.setType("text/plain");
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(Intent.EXTRA_TEXT, textToShare);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
Then in the onOptionsItemSelected method, call this function when the menu item is clicked :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here.
int id = item.getItemId();
if (id == R.id.action_share) {
share();
return true;
}
return super.onOptionsItemSelected(item);
}
Also make sure you've overriden onCreateOptionsMenu in your Activity like this :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
Here's the output :
screenshot from an android device
In Android OS Lollipop google has change their share intent design. So to get this design you should have lollipop version and above.
Update:
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpg");
File photoFile = new File(getFilesDir(), "foo.jpg");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(photoFile));
mShareActionProvider.setShareIntent(shareIntent);
}
}
try like this.

ShareActionProvider from support.v7 not clicking

I am trying to get a working "share" button on the title bar. Initially, I followed the advice on developers.android.com for this, but got an exception saying that I needed to use MenuItemCompat. I am doing so, but now my share button won't even "click".
My relevant code is:
public boolean onCreateOptionsMenu(Menu menu) {
Log.d("CYCLE:","onCreateOptionsMenu()");
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_to_do_list, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.action_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
setShareIntent();
mShareActionProvider.setShareHistoryFileName("my_share_history.xml");
return true;
}
public void setShareIntent(){
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "some text to share");
mShareActionProvider.setShareIntent(shareIntent);
}
and my menu item in XML is:
<item android:id="#+id/action_share"
android:title="Share"
app:showAsAction="ifRoom"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>
am I missing something to make this button be clickable? (I am not even talking about functionality yet. Simply the ability to be clicked (you know, like with the clicking/tapping animation).
thanks.
Have you set share Intent? Without the share Intent, Android would not know what exactly need to be shared by the provider.

Share icon not showing in ActionBar

I am trying to use ShareActionProvider as in http://developer.android.com/training/sharing/shareaction.html but my ActionBar does not show the share icon. Instead, the overflow button is shown which a menu item "share" (the rest works well). I am using this code:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:compat="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_item_share"
android:title="share"
compat:showAsAction="ifRoom|collapseActionView"
android:actionProviderClass="android.widget.ShareActionProvider"/>
</menu>
In my activity, I have:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.menu_detail, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
// Return true to display menu
return super.onCreateOptionsMenu(menu);
}
// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
And my theme is:
<style name="Theme.Base" parent="android:Theme.Light" />
<style name="AppTheme" parent="Theme.Base">
<item name="android:actionModeCloseDrawable">#drawable/ic_action_back</item>
</style>
Why is does the share icon not show up?
Some of your code (compat:showAsAction) is written to use the appcompat-v7 backport of the action bar.
Some of your code (getActionProvider(), Theme.Light) is written to use the native action bar.
Pick one and stick with it.
My guess is that more of your current code is set up for the native action bar, in which case changing compat: to android: in your menu XML resource will probably get your ShareActionProvider working.

ShareActionProvider doesn't share text from Intent with Facebook

I'm using a ShareActionProvider(android.widget.ShareActionProvider) to share simple text.
It works fine with Gmail, WhatsApp etc but not with Facebook...
It doesn't share the text attached to the Intent, instead it just ask the user to write a new post.
How to solve this?
Here is my code:
XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
>
<item android:id="#+id/action_share"
android:title="#string/action_share"
android:showAsAction="always"
android:actionProviderClass="android.widget.ShareActionProvider" />
</menu>
Java:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.detailfragment, menu);
// Retrieve the share menu item
MenuItem share = menu.findItem(R.id.action_share);
// Get the provider and hold onto it to set/change the share intent.
mShareActionProvider = (ShareActionProvider) share.getActionProvider();
// Attach an intent to this ShareActionProvider. You can update this at any time,
// like when the user selects a new piece of data they might like to share.
if (mShareActionProvider != null && mForecastStr != null ) {
mShareActionProvider.setShareIntent(createShareIntent());
Log.v(LOG_TAG, "mForecast: " + mForecastStr);
Log.v(LOG_TAG, "Intent: " + mShareActionProvider);
} else {
Log.d(LOG_TAG, "Share Action Provider is null?");
}
}
public Intent createShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, mForecastStr);
return shareIntent;
}
Thanks!!
Unfortunately, you can't solve it. Facebook does not handle the EXTRA_TEXT field. You can check this bug report page.
There is a middle solution. It is true FB doesn't share TEXTS. They say that it is an attack to the freedom of expression of the User. But, you have a solution. You may pass a link. Then FB translate the "Intent" to a image and a link, and it added to the future response. But it leave blank the User writing space...
For example try:
shareIntent.putExtra(Intent.EXTRA_TEXT, "http://www.google.com");

Adding a "share" button to share the app on social networks

I have an app, and I'd like to add a share button to it. Once the button is clicked, I'd like it to open the following window:
Then the user will choose where to share it and it will display the following default message:
"Just found this great app! Find it here: https://play.google.com/store/apps/details?id=com.ideashower.readitlater.pro"
Solution 1: Launch ACTION_SEND Intent
When launching a SEND intent, you should usually wrap it in a chooser (through createChooser(Intent, CharSequence)), which will give the proper interface for the user to pick how to send your data and allow you to specify a prompt indicating what they are doing.
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
# change the type of data you need to share,
# for image use "image/*"
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, URL_TO_SHARE);
startActivity(Intent.createChooser(intent, "Share"));
Solution 2: Use ShareActionProvider
If you are just looking to add a Share button in Overflow Menu, also have a look at ShareActionProvider.
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.share, menu);
MenuItem item = menu.findItem(R.id.share_item);
actionProvider = (ShareActionProvider) item.getActionProvider();
// Create the share Intent
String shareText = URL_TO_SHARE;
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain").setText(shareText).getIntent();
actionProvider.setShareIntent(shareIntent);
return true;
}
Hope this helps. :)
As explained on Android Developers on this link: http://developer.android.com/training/sharing/shareaction.html
you have to add this menu item:
<item
android:id="#+id/menu_item_share"
android:showAsAction="ifRoom"
android:title="Share"
android:actionProviderClass=
"android.widget.ShareActionProvider" />
Then add the following code in Activity:
private ShareActionProvider mShareActionProvider;
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.share_menu, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
// Return true to display menu
return true;
}
// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}

Categories

Resources