I can get Facebook profile images using
http://graph.facebook.com/<facebookId>/picture?type=square
but it redirects to
https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpf1/t1.0-1/p50x50/XXX.jpg
How can I set new image url using Loopj's Android Smart Image View?
You can the direct url first using the following code :
public String GetDirectURL(String url_send) {
URL url;
URL secondURL = null;
try {
url = new URL(url_send);
HttpURLConnection ucon = null;
try {
ucon = (HttpURLConnection) url.openConnection();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ucon.setInstanceFollowRedirects(false);
secondURL = new URL(ucon.getHeaderField("Location"));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
return secondURL.toString();
}
and then assign the url returned to smart image view object ,but to not to be pixelated change type of image to large or xlarge
Related
I have written the below program in AsyncTask to load image from internet and show in ImageView. The program works fine if I give any direct image link, but don't work with API links.
What I mean is, for example, to have the cover of Farmer Boy from OpenLibrary, I need to give below source in html or in browser:
http://covers.openlibrary.org/b/isbn/9780385533225-S.jpg
However, if I enter above link in browser, the browser redirect to below address.
http://ia700804.us.archive.org/zipview.php?zip=/12/items/olcovers4/olcovers4-M.zip&file=49855-M.jpg
My problem is, my code works with last one, but not with the first one.
How can I get the image (in my android application) using the first link?
CODE:
private class getImageOpenLibrary extends AsyncTask<String, Void, Bitmap>
{
protected Bitmap doInBackground(String... args) {
URL newurl = null;
try {
//newurl = new URL("http://covers.openlibrary.org/b/isbn/"+args[0]+"-M.jpg"); // THIS DOES NOT WORK, args[0] = 9780064400039
newurl = new URL("http://ia700804.us.archive.org/zipview.php?zip=/12/items/olcovers4/olcovers4-M.zip&file=49855-M.jpg"); //THIS WORKS
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap mIcon_val = null;
try {
mIcon_val = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mIcon_val;
}
//#Override
protected void onPostExecute(Bitmap result1)
{
ImageView mImageView = (ImageView) findViewById(R.id.cover);
mImageView.setImageBitmap(result1);
}
}
You should handle the redirection. The url redirects to another URL. You should open a second connection on the redirect URL. To be able to get the redirect URL, set setInstanceFollowRedirects to false on the connection and read the Location in the header fields.
URL url = new URL("http://covers.openlibrary.org/b/isbn/9780385533225-S.jpg");
HttpURLConnection firstConn = (HttpURLConnection) url.openConnection();
firstConn.setInstanceFollowRedirects(false);
URL redirectURL = new URL(firstConn.getHeaderField("Location"));
URLConnection redirectConn = redirectURL.openConnection();
Bitmap bitmap = BitmapFactory.decodeStream(redirectConn.getInputStream());
I have images on server named by email so when i try to download them on my application the # symbol not shown on the path, is there anyway to stop escape this symbol ?
Example :
Correct Path
Http://www.xxxxx.com/a#a.com.jpg
Wrong Path
Http://www.xxxxx.com/aa.com.jpg
I tried URL encode but its not useful in my case
Bitmap downloadFile(String fileUrl) {
URL myFileUrl = null;
Bitmap bmImg = null;
try {
myFileUrl = new URL(fileUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
// imView.setImageBitmap(bmImg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bmImg;
}
Try using the URLEncoder from Android SDK.
try {
myFileUrl = new URL(java.net.URLEncoder.encode(fileUrl));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
EDIT:
Just saw my mistake. That won't work because it's encoding the entire URL.
You have to separate the email address from the rest of the url like this.
myFileUrl = new URL("http://www.xxxx.com/"+java.net.URLEncoder.encode(email));
Or alternatively, just replace the problem character.
myFileUrl = new URL(fileUrl.replace("#","%40"));
I needed to load remote images to my application and bind this image to an QuickcontactBadge object. HttpURLConnection is used to download the image data and BitmapFactory is used to produce the bitmap which will be used as imageview resources.
But I dont know exactly how yo do this?
Can anyone help me over this?
Thanks
This could help you.
Bitmap bmImg;
void downloadFile(String fileUrl){
URL myFileUrl =null;
try {
myFileUrl= new URL(fileUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
imView.setImageBitmap(bmImg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Source : http://en.androidwiki.com/wiki/Loading_images_from_a_remote_server
See this too
http://ballardhack.wordpress.com/2010/04/05/loading-remote-images-in-a-listview-on-android/
I used the code as follows to show up facebook avatar to ImageView
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView img = (ImageView) findViewById(R.id.imgAvatar);
img.setImageBitmap(getBitmapFromURL("http://graph.facebook.com/"+"100002394015528"+"/picture"));
}
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}`
But does it not work. Please help me.
http://graph.facebook.com/id/picture doesn't return an image. It returns some response headers including a 302 redirect, and a location header.
Your example for instance redirects to: http://profile.ak.fbcdn.net/hprofile-ak-snc4/211619_100002394015528_568817_q.jpg
So instead of
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
You need to get the headers from the request, follow the location and then do what you were doing before. I don't know Android, or what language that is. (Java?) So I can't help with that, but I think this might be enough information to get you headed in the right direction.
Use this function for get real URL to avatar:
public static String getUrlFacebookUserAvatar(String name_or_idUser )
{
String address = "http://graph.facebook.com/"+name_or_idUser+"/picture";
URL url;
String newLocation = null;
try {
url = new URL(address);
HttpURLConnection.setFollowRedirects(false); //Do _not_ follow redirects!
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
newLocation = connection.getHeaderField("Location");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return newLocation;
}
Can anyone please provide me some idea/guidance on how to save an image from a webserver and set it as wallpaper? i am developing an android application which needs to do that and i am new in android. Thanks a lot.
I had tried writing my own code but it doesn't work as i can't find my images after download but the wallpaper has change to the downloaded picture. here is my existing code.
Bitmap bmImg;
void downloadFile(String fileUrl) {
URL myFileUrl = null;
try {
myFileUrl = new URL(fileUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
int length = conn.getContentLength();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
// this.imView.setImageBitmap(bmImg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
String filepath=Environment.getExternalStorageDirectory().getAbsolutePath();
FileOutputStream fos = new FileOutputStream(filepath + "/" + "output.jpg");
bmImg.compress(CompressFormat.JPEG, 75, fos);
fos.flush();
fos.close();
Context context = this.getBaseContext();
context.setWallpaper(bmImg);
} catch (Exception e) {
//Log.e("MyLog", e.toString());
TextView tv = (TextView) findViewById(R.id.txt_name);
tv.setText(e.toString());
}
}
I had tried writing my own code but it
doesn't work as i can't find my images
after download. here is my existing
code.
Your code would save the image in the data/data/<your_app_package_name> folder of the phone. You can then use either a WallpaperManager instance or do a context.setWallpaper(bitmap)(this is deprecated) to set your bitmap as the wallpaper.