facebook API add image along with wall feed - android

It is possible to post to a Facebook wall a message with image (not an image link, but image data)?
I did not find this possibility either in http://developers.facebook.com/docs/reference/api/post/ or in https://developers.facebook.com/docs/guides/attachments/.
And I was ready to put up with impossibility of doing it, but I came across documentation for SLComposeViewController class introduced in iOS 6.0 (http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Reference/SLComposeViewController_Class/Reference/Reference.html).
This class has a method - (BOOL)addImage:(UIImage *)image that does exactly what I need.
I program for Android and hence I cannot use it. But obviously this method must use facebook API. But I cannot find it: everything related to image posting requires url, not data.
So, is is possible in Android to post to a Facebook wall a message with image data?

EDIT: I posted the answer almost with your comment. You can use something like this to cast you Image from assets into a Bitmap.
InputStream bitmap = null;
try {
bitmap = getAssets().open("icon.png");
bmpImageGallery = BitmapFactory.decodeStream(bitmap);
} catch (IOException e) {
e.printStackTrace();
} finally {
bitmap.close();
}
This is how I display an Image from the Gallery via an Intent in the onActivityResult method:
targetURI = data.getData();
try {
bmpImageGallery = MediaStore.Images.Media.getBitmap(this.getContentResolver(), targetURI);
// SET THE IMAGE FROM THE GALLERY TO THE IMAGEVIEW
imgvwSelectedImage.setImageBitmap(bmpImageGallery);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
This is the code to upload the image:
byte[] data = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmpImageGallery.compress(CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();
Bundle postImgGallery = new Bundle();
// ADD THE PHOTO DATA TO THE BUNDLE
postImgGallery.putByteArray("photo", data);
// ADD THE CAPTION FROM THE STRING finalStatusMessage TO THE BUNDLE
if (finalStatusMessage.equals("")) {
/***** DO NOTHING HERE *****/
} else {
postImgGallery.putString("caption", finalStatusMessage);
}
Utility.mAsyncRunner.request(userID + "/photos", postImgGallery, "POST", new PhotoUploadListener(), null);
NOTE: In this bit here "caption", finalStatusMessage, the caption can also be substituted with message. I have never seen any difference in the posts using either of these. But do check before using either, just to be safe. ;-)
This class is used to check the status of the upload:
private class PhotoUploadListener extends BaseRequestListener {
#Override
public void onComplete(String response, Object state) {
// DISPLAY A CONFIRMATION TOAST
}
}

Related

I am facing java.net.MalformedURLException: no protocol: While converting url to bitmap in android

I am trying to convert url to bitmap and then set that bitmap to background as wallpaper. And all this process is getting done in background with the use of worker class in android. But I am getting no protocol error. I am fetching any one random wallpaper link from firebase then I am trying to convert it into bitmap by
try {
URL url = new URL(image_url);
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
myWallpaperManager.setBitmap(image,null,false,WallpaperManager.FLAG_SYSTEM);
} catch(IOException e) {
Log.e("tag102",e.getMessage());
}
But this method gave me this error in logcat
2022-07-08 03:15:07.544 25513-25752/com.nightowl.stylo E/tag102: no protocol:
But when I put hardcoded string (showed in below method) in url method parameter then it return bitmap without any error. and wallpaper also gets changed
try {
URL url = new URL("https://images.pexels.com/photos/6336035/pexels-photo-6336035.jpeg?auto=compress&cs=tinysrgb&fit=crop&h=1200&w=800");
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
myWallpaperManager.setBitmap(image,null,false,WallpaperManager.FLAG_SYSTEM);
} catch(IOException e) {
Log.e("tag102",e.getMessage());
}
But i want random string to set and get me bitmap from that. So how i can achieve that? I also try to encode url by
try {
encodedURL = URLEncoder.encode(image_url, "UTF-8");
} catch (UnsupportedEncodingException e) {
Log.e("tag3",e.toString());
}
I am using three kind of url in this project
https://images.pexels.com/photos/6336035/pexels-photo-6336035.jpeg?auto=compress&cs=tinysrgb&fit=crop&h=1200&w=800
https://cdn.pixabay.com/photo/2020/11/27/22/07/naruto-5783102_960_720.png
https://images.unsplash.com/photo-1641414315243-196e7382c32d?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1178&q=80
Any help will be appreciated.

How to get the Profile image of Participant in RealTimeMultiplayer using Google Play Game Services

I am building a game using google play game services on RealTimeMultiplayer concept, my questions is how to get access the all participants profile image to display. i am new to the google play game services, not found a solutions or any guidance on API. please guide me, can we access and how to access?
Using the ImageManager class to load the ImagerUri is how I tackle it.
Below, I loop through each Participant in the currentRoom, and request their IconImageUri to be loaded by an ImageManager instance.
This can directly be used to inflate a view, but as I am using LibGDX, I save it as png for it to handle.
(You can see that I tell theGameInterface that it is null if their is no pic, either because there isn't one, or they are an "unknown" participant and therefore GPGS returns null)
for (Participant p : mRoomCurrent.getParticipants()) {
//makeToast("should be loading pic");
ImageManager IM = ImageManager.create(this);
IM.loadImage(new ImageManager.OnImageLoadedListener() {
#Override
public void onImageLoaded(Uri arg0, Drawable drawable, boolean arg2) {
if(drawable == null) {
theGameInterface.picLoaded(participantID, null);
} else {
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
try {
FileOutputStream out = openFileOutput("pic" + participantID + ".png", Context.MODE_MULTI_PROCESS);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
//dLog(e.getStackTrace().toString());
}
}
}
}, p.getIconImageUri());
These were deduced from developer site
ImageManager
and
Participant

How to get image from an HTML page in Android

I use the following code do get an image from a regular image url:
try
{
url = new URL("http://example.com/test.jpg");
final Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
final ImageView imageView=(ImageView)findViewById(R.id.imageView2);
MainActivity.this.runOnUiThread(new Runnable()
{
#Override
public void run()
{
imageView.setImageBitmap(bmp);
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
However, I want to download images that are displayed by html or php pages.
url = new URL("http://example.com/showimage.php");
If I use this, I get the following message:
SkImageDecoder::Factory returned null
How should I modify my code?
I recommend you to look at this library: http://jsoup.org/
This seems to be the solution when the page is redirected: http://blog.kosev.net/2011/01/follow-302-redirects-with.html

Android Facebook API feed dialog?

Ok so I have setup a new Facebook app and I have the API and all that jazz setup also and have made successful posts to Facebook using the API. Here is my issue.
How do I format the text the way I want too??
For example:
description += "First line \r\n";
description += "Second line \r\n";
I am using the Facebook Android SDK and using the Feed Dialog approach as listed here
Also, I am using the feed dialog like so:
String description += "First line \r\n";
description += "Second line \r\n";
Facebook facebook = new Facebook("MY_APP_ID");
SetAccessToken(mContext, facebook);
params.putString("link", "");
params.putString("picture", "");
params.putString("name", "");
facebook.dialog(mContext, "feed", params, new Facebook.DialogListener() {
public void onFacebookError(FacebookError e) {
}
public void onError(DialogError e) {
}
public void onComplete(Bundle values) {
}
public void onCancel() {
}
});
Whenever, I do that it pulls up my Facebook dialog and the lines are not separated by line feeds. I also tried to use HTML such as
<p>Line 1</p>
<p>Line 2</p>
My guess is that I need to use another method in order to actually post formatted text, other than the feed dialog. I know that other Facebook apps are posting to users feeds with properly formatted text. Just not sure why the SDK doesn't offer the same functionality.
EDIT 1
Just to add this, the submission is actually in a URL format at the end anyway. I tried adding URL escape characters but that didn't seem to do anything for me.
String url = endpoint + "?" + Util.encodeUrl(parameters);
Then they pass that into a webview. So I am currently looking into this. Any advice is appreciated.
as far as I have researched...facebook doesn't allow it's domain image to be posted on wall through post. So, we can't use it. Now what I am doing is getting the imge through following code:
ImageView user_picture;
user_picture = (ImageView) findViewById(R.id.user_picture);
URL img_value = null;
Bitmap mIcon1 = null;
try {
img_value = new URL("http://graph.facebook.com/XXXX/picture");
try {
mIcon1 = BitmapFactory.decodeStream(img_value
.openConnection().getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(mIcon1!=null){
user_picture.setImageBitmap(mIcon1);
}
if you want it to convert to byte array
ByteArrayOutputStream baos = new ByteArrayOutputStream();
mIcon1.compress(Bitmap.CompressFormat.PNG, 100, baos);
b = baos.toByteArray();
Sending this image to my server and then posting to wall.
Have you tried:
String message = "<p>Line 1</p><p>Line 2</p>";
params.putString("message", Html.fromHtml(message).toString());

How to upload Image and tweet the message without using TweetPic for twitter?

In my application i am using tweetPic to uploaded image but with the image is only able to see on the tweetPic.
Instead of that i want to upload the picture on the twitter and also with the custom message. So how it is possible?
Twitter OAuth is also required for that.
I want any demo or sample example app that done like that.
Thanks.
Well i have search many but still not get the answer as i want.
Finally i use this to upload the photo on Twitter with the Custom Message:
File picture = new File(APP_FILE_PATH + "/"+filename+".jpg");
// Create TwitPic object and allocate TwitPicResponse object
TwitPic tpRequest = new TwitPic(TWITTER_NAME, TWITTER_PASSWORD);
TwitPicResponse tpResponse = null;
// Make request and handle exceptions
try {
tpResponse = tpRequest.uploadAndPost(picture, customMessageEditText.getText()+" http://www.MySite.com/");
}
catch (IOException e) {
e.printStackTrace();
}
catch (TwitPicException e) {
e.printStackTrace();
}
// If we got a response back, print out response variables
if(tpResponse != null) {
tpResponse.dumpVars();
System.out.println(tpResponse.getStatus());
if(tpResponse.getStatus().equals("ok")){
Toast.makeText(getApplicationContext(), "Photo posted on Twitter.",Toast.LENGTH_SHORT).show();
}
}
Still in search of the Twitter Demo to tweet pics on the Twitter with custom message with twitter OAuth and Without using twitPic.
Enjoy. :)
Thanks.

Categories

Resources