How To Get Clear Image From FaceBook - android

I used graph API Json Response of Facebook Wall Post Images and display in my APP i successfully got it. But the wall images look very Blur how to resolve? i used this code for get wall picture
URL url=new URL(hashMap.get("picture_url"));
bitmap=BitmapFactory.decodeStream(url.openConnection().getInputStream());
((ImageView)view.findViewById(R.id.imageView_FullImage)).setImageBitmap(bitmap)

The Facebook Graph API as well as the FQL data set always returns the Picture URL of a thumbnail. If you look at the URL it returns, it will have one of these ending (right before the image extension .jpg, .png, etc) _t., _a.. For example, if the URL is to a JPG file, it could have an ending _t.jpg
The idea is to swap the ending and choose a normal size for the image that is returned. To do this, use the code below that will replace the endings with the one for normal sized images (that should have the _n.)
By the way, I don't think the tag you are looking for is picture_url. It should be just picture. But regardless, get the source URL as shown below, replace the endings and then pass it to the this line in your code:
// THIS SHOULD BE AFTER THE if....else code block
bitmap=BitmapFactory.decodeStream(url.openConnection().getInputStream());
CODE TO REPLACE THE VARIOUS THUMBNAIL IMAGES: By the way, this is production code and works perfect.
String PICTURE_URL;
String getPicture = JOFeeds.getString("picture");
if (getPicture.contains("_t.")) {
PICTURE_URL = getPicture.replaceAll("_t.", "_n.");
} else if (getPicture.contains("_a.")) {
PICTURE_URL = getPicture.replaceAll("_a.", "_n.");
} else if (getPicture.contains("_s.")) {
PICTURE_URL = getPicture.replaceAll("_s.", "_n.");
} else if (getPicture.contains("_q.")) {
PICTURE_URL = getPicture.replaceAll("_q.", "_n.");
}
Note: However, in some cases, like a Video preview or a Link preview, it will not always have a bigger image available. Nothing much you can do about it nor can Facebook I suspect. These typically come from posts that are shared by users from other websites.

Related

Android: How To Fix Blurry Google Profile Image Taken From Firebase?

The scenario:
I am building an app that allows the user to sign in using their Google account, it's worth noting that this is done using Firebase authentication.
The problem:
The user signs in perfectly but whenever i get their profile picture it is returned fine but it's very blurry.
I've researched and found some sites mentioning to change the resolution of the Google profile image but I'm not sure if that's possible. I know Facebook has a graph that allows for you to manipulate the URL of the profile image to change the resolution but I'm not sure how to improve the resolution of the profile image via Google.
I've posted a screenshot of the blurry image below:
This is the XML code for the ImageView that I'm loading the profile image in:
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/image_view_profile_pic"
android:layout_width="96dp"
android:layout_height="96dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="36dp"
android:src="#drawable/app_bar_tool_bar_bg"
app:civ_border_width="2dp"
app:civ_border_color="#FFFFFFFF"
android:contentDescription="#string/content_description_profile_picture" />
This is the code I used to get the profile image from Google via Firebase:
private void getUserProfileContent() {
// Check if the user is signed in
if (mUser != null) {
for (UserInfo profile : mUser.getProviderData()) {
// Profile photo url
Uri photoUrl = profile.getPhotoUrl();
// Load user's profile photo from their signed-in provider into the image view
Glide.with(ProfileActivity.this)
.load(photoUrl)
.into(mProfilePic);
}
}
}
This is the code to get the currently authenticated user:
// Variable declaration
private FirebaseUser mUser;
// Initializatioin in onCreate method
mUser = FirebaseAuth.getInstance().getCurrentUser();
Then of course, I call the above method in the onCreate method so it gets executed, like so:
getUserProfileContent();
I have read on this site that Google URLs can be manipulated to change the size of an image:
Details about Google's "magic" URL
Also, I've seen this site mentioning something similar about profile picture being blurry:
Firebase Google Group: Profile picture blurry
As it relates to the second link I haven't quite wrapped my head around how to carry out what they've suggested.
I solved my own problem, I will explain in full detail, see below.
Solution:
This is the newly modified code to my method in the question above, this is the fix:
private void getUserProfileContent() {
// Check if the user is signed in
if (mUser != null) {
for (UserInfo profile : mUser.getProviderData()) {
// Get the profile photo's url
Uri photoUrl = profile.getPhotoUrl();
// Variable holding the original String portion of the url that will be replaced
String originalPieceOfUrl = "s96-c/photo.jpg";
// Variable holding the new String portion of the url that does the replacing, to improve image quality
String newPieceOfUrlToAdd = "s400-c/photo.jpg";
// Check if the Url path is null
if (photoUrl != null) {
// Convert the Url to a String and store into a variable
String photoPath = photoUrl.toString();
// Replace the original part of the Url with the new part
String newString = photoPath.replace(originalPieceOfUrl, newPieceOfUrlToAdd);
// Load user's profile photo from their signed-in provider into the image view (with newly edited Url for resolution improvement)
Glide.with(ProfileActivity.this)
.load(newString)
.into(mProfilePic);
} // End if
} // End if
}
Explanation:
The code is self explanatory but for the sole purpose of clarity for many others who were having issues with this, I basically swapped out the part of the original Url that I retrieved from Google that specified the image's resolution with a higher resolution of my own.
Replaced this "s96-c/photo.jpg" with "s400-c/photo.jpg"
I have provided a screenshot below with TextViews displaying:
Url path of the original Url retrieved from Google (this one has the s96-c in its url)
Url path of the newly modified Url by me (this one has the s400-c in its url)
Original Url retrieved from Google with toString() method called on it. This is the third Url in the screenshot with s96-c
Newly modified Url by me with toString() method called on it. This is the fourth Url (similarly to the 3rd Url in the screenshot this Url has s96-c in its Url String)
In that order
Note:
I wanted to do an in depth explanation with this answer because I wanted to show that even though the string with low res was replaced with high res version, you'll notice the last TextView displays in its Url s96-c and the second-to-last Url (granted you're looking from top to bottom) displays the exact same thing, s96-c
BUT
The resolution indeed improved, so my theory is that even though the resolution improved, by me using a higher value, Google's system returned the improved image with a size of s96-c regardless (Let me know if anyone has a more concrete theory)
Screenshot of profile image with the blurriness fixed (improved resolution):
Anyway there you have it, I wanted to show a screenshot with the Urls and their differences so that I can explain how everything was happening, for anyone who was curious.
I hope this helps many others, because the blurry Google user profile image was a very inconvenient problem for myself.
In my case, inside of the HTML of my Ionic4 component I used:
<img class="avatar" [src]="user.photoURL + '?height=150'">
Adding the height parameter to the end of the URL worked for me.
Easy Fix
Just replace s96-c with s400-c or any other like s300-c
from the image url you get from google.
Example:
Here you can easily see the difference..
Look at the end of url.
https://lh4.googleusercontent.com/-C0Mqxb50Hxg/AAAAAAAAAAI/AAAAAAAADcw/xuG-pk0PzaI/s96-c/photo.jpg
https://lh4.googleusercontent.com/-C0Mqxb50Hxg/AAAAAAAAAAI/AAAAAAAADcw/xuG-pk0PzaI/s400-c/photo.jpg
I feel the code is much clearer this way
private void onSignedInInitialize(FirebaseUser user) {
if(user.getDisplayName() != null && user.getDisplayName().length() > 0)
{
usernameText.setText(user.getDisplayName());
}
String photoUrl;
String provider = user.getProviders().get(0);
if (provider.equals("facebook.com")) {
photoUrl = user.getPhotoUrl() + "?height=500";
}
else if(provider.equals("google.com"))
{
photoUrl = user.getPhotoUrl().toString();
//Remove thumbnail url and replace the original part of the Url with the new part
photoUrl = photoUrl.substring(0, photoUrl.length() - 15) + "s400-c/photo.jpg";
}
else
{
photoUrl = user.getPhotoUrl().toString();
}
Picasso.with(this)
.load(photoUrl)
.placeholder(R.drawable.profile_avatar)
.error(R.drawable.profile_avatar)
.into(circleImageView);
}
Instead of replacing the size, you can also just remove =s96-c from the photoUrl, and you will be receive the max size.

How to get the image content url from Dropbox without expiration?

I am trying to get the image URL from the Dropbox and I followed the below code from the documentation.
try {
Entry directory = dropboxApi.metadata("/", 1000, null, true, null);
directory.mimeType = "image*//*";
for (Entry entry : directory.contents) {
DropboxAPI.DropboxLink link = dropboxApi.media(entry.path, false);
if (link.url.contains(".jpeg") || link.url.contains(".png") || link.url.contains(".jpg"))
files.add(link.url);
}
} catch (DropboxException e) {
e.printStackTrace();
}
Based on this code I can get the image URL. But it is not working after hours.
In my scenario, I want to get the image URL and I will send it to server API. So when I retrieve other API, I will get the image URL, what I sent. Once I got the URL, just I want to set it to imageview.
I have verified this link. I got the different link by using share method. But I don't know how to set that into my ImageView, whereas by using media method I can get the URL and I can set it to ImageView.
Please can someone help me to solve this problem
If i make an Http request for share url then I am getting image name with download option.
I am getting 16 headers.
From this am not getting any clue. There is no header name contains Location.There is one content-piracy-policy which gives some url values combined. But it is also not relevant.

JPG image shows from phone but not from server - android

In my app I am able to pick up a picture from the Gallery and show it on my phone. The picture has extension JPG. But when I email it to myself, save it on the server and then try to display it on my phone, it does not display. I even tried to downsize it to 30% using my email app on the phone, so now it is 220KB instead of 1.4MB but it still does not display.
In both cases I use the method
imageView.setImageBitmap(BitmapFactory.decodeFile(personPicture))
What do I need to do to overcome this problem?
BTW: the name of the picture was changed when I saved it on the server. I do not think it matters but I mentioned it anyway.
EDIT
The above is all the code I am using. Just to complete the issue here is the code that handles both jpg and png and it works if the picture is renamed to png.
if (url.contains("jpg")) {
imageView.setImageBitmap(BitmapFactory.decodeFile(url));
} else {
Drawable drw = LoadImageFromWebOperations(url);
if (drw != null) {
imageView.setImageDrawable(drw);
}
}
Note: the 1.4MB PNG file worked fine on the emulator but gave Out of memory exception on the device. When I re-sized the PNG file to 350KB it displayed properly on the device also.
If needed here is the url used in the above code (a picture of a cat)/
http://212.150.56.58:8080/feedback/pictures/56.png
When you try to load image from server into app, load it using Picasso library like as below:
Picasso.with(MainActivity.this).load("image_to_be_loaded").into(profile_image);
Edit
If you don't want to use third party library then try the following code:
public Drawable loadImageFromURL(String url, String name) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, name);
return d;
} catch (Exception e) {
return null;
}
}
Use Picasso to load server images to your app image view it also supports caching and lot many features.
Use is like this
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
checkout Picasso Library for more details.
I do not understand why, but if I renamed the picture on my server to extension PNG the picture displays, in all sizes.

Get Larger Image From Facebook Post?

I'm currently getting the newsfeed of a Facebook user. Currently, I can only retrieve thumbnail images that are associated with links, rather than the full-size image. Does anyone know how to get a larger image?
I've looked at the following posts and tried their solutions; however, the posts seem outdated and the facebook graph api seems to have changed since then:
Facebook Graph API Change: Picture type (size) no longer working?
Facebook API post: How to get larger thumbnail?
Please note that I'm on the me/home edge, so the answer below returns an error. See comments below.
my code is:
params.putString("fields","id,actions,application,caption,created_time,description,from,link,message,message_tags,"
+ "name,object_id,picture,place,source,status_type,story,updated_time,type,status_type,story,to,type,"
+ "updated_time,with_tags");
Change to this:
params.putString("fields","id,actions,application,caption,created_time,description,from,link,message,message_tags,"
+ "name,object_id,full_picture,place,source,status_type,story,updated_time,type,status_type,story,to,type,"
+ "updated_time,with_tags");
Here picture.height(1000) will give you a large image. and you can put your own required size but it is not supported in me/home i have searched a lot.
For Testing purposre use this link :Facebook graph explorer.
Eventually I came to this solution:
1) find the URL of the larger image (see code below)
2) get bitmap from URL and add to application UI.
The code mentioned above:
private String getRealURL(String smallURL){
if (smallURL.contains("url=http")){
String[] pieces = smallURL.split("url=");
pieces[1].replace("%2F", "//");
pieces[1].replace("%3A", ":");
Log.e("RealURL1:", pieces[1]);
return pieces[1];
}
else{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.setLength(0);
stringBuilder.append("https://graph.facebook.com/");
stringBuilder.append(item.getObjectID());
stringBuilder.append("/picture?type=normal&access_token=");
stringBuilder.append(Session.getActiveSession().getAccessToken());
Log.e("RealURL2:", stringBuilder.toString());
return stringBuilder.toString();
}
}
Simply use this field ?field=full_picture instead of picture.
?field=full_picture is the only way to get it to work now. I have been searching everywhere, and finally found the answer here. picture field doesn't support subfields any more so you can't use picture.width.height any more

how to get clear image on Facebook Wall

I used graph API Json Response of facebook Wall POst Images and display in my APP i successfully got it. But the images look very Blur .. please tel me how to get clear images as like in Facebook
here is my code
URL url=new URL("http://graph.facebook.com/"+hashMap.get("id")+"/picture?type=normal");
Bitmap bitmap=BitmapFactory.decodeStream(url.openConnection().getInputStream());
((ImageView)view.findViewById(R.id.imageView_ProfilePicture)).setImageBitmap(bitmap);
url=new URL(hashMap.get("picture_url"));
bitmap=BitmapFactory.decodeStream(url.openConnection().getInputStream());
((ImageView)view.findViewById(R.id.imageView_FullImage)).setImageBitmap(bitmap);
And in AsynTask
dobackground(){
hashMap.put("picture_url", data.getJSONObject(i).getString("picture"));
}
Use "/picture?type=large" instead of "/picture?type=normal" to get a slightly bigger picture.
While ?type=large provides a bigger image it may still not be large enough for a xhdpi display. You can actually specify a actual size now and they'll try their best to fulfill it.
For example this gives you a large square of Mark Zuckerberg:
http://graph.facebook.com/4/picture?width=500&height=500

Categories

Resources