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());
Related
Given the URL of a website I want to retrieve the logo and background image of the website and display it in an android app. For example from this link I would want the background image and the hack western logo.
EDIT: I forgot to mention I am trying to achieve this effect for a list of URL's of undetermined size until run time
Use this AsyncTask for loading image from URL
public class LoadImageTask extends AsyncTask<Void, Void, Void>{
private Bitmap mMyBitmap;
#Override
protected Void doInBackground(Void... params) {
try {
URL url = new URL("https://hackwestern.com/images/logo_vert_spons.png");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
mMyBitmap = BitmapFactory.decodeStream(input);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
mImageView.setImageBitmap(mMyBitmap);
}
}
Then call it on OnCreate or somewhere you need:
new LoadImageTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
I am trying to download images from the net in my android application, it works most of the times but some pictures are failing to download and the Bitmap is null. The link to the images is always being there however. Any ideas what is causing this?
private class GetImage extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... urls) {
int len = 500;
try {
URL url = new URL(urls[0]);
Log.v("url",urls[0]);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.connect();
int response = connection.getResponseCode();
inputstream = connection.getInputStream();
System.out.println(inputstream.toString());
bitmap = BitmapFactory.decodeStream(inputstream);
if(bitmap==null)
Log.v("Bitmap","fail");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (inputstream != null) {
try {
inputstream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return "some string since it bitmap is sometimes null";
You can't always count on the network. You can use an http library like okhttp or volley that will allow you to do retries easily (or you could just retry yourself). Those libraries do a bunch of things to smooth out the http experience over using raw HttpUrlConnection.
Depending on how critical the image is, you could always hide the image in onPostExecute if it fails.
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
I'm trying to get the app to show an image from a URL, im fairly certain the issue is with the AsyncTask but I've returned to this code several times over the past week and I still cant see where I'm going wrong.
The Internet permission is set and I am getting no LogCat
ImageView eventImage2;
eventImage2 = (ImageView) findViewById(R.id.eventImage2);
new imageupdate().execute();
public class imageupdate extends AsyncTask<Bitmap, Void, Bitmap> {
#Override
protected Bitmap doInBackground(Bitmap... url) {
URL url1;
try {
url1 = new URL("http://masterzangetsu.eu/Apps/NowIGetYou/banner.png");
HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
InputStream is = connection.getInputStream();
Bitmap img = BitmapFactory.decodeStream(is);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return img;
}
protected void onPreExecute(String result) {
}
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
eventImage2.setImageBitmap(result);
}
}
As far as I can tell the img variable defined with the
img = BitmapFactory.decodeStream(is);
isnt being linked to the variable being returned
return img;
Both variables result and img are coming back as null
Change this
Bitmap result = null;
InputStream is = connection.getInputStream();
Bitmap img = BitmapFactory.decodeStream(is);
img = result;
to
Bitmap img = null;
InputStream is = connection.getInputStream();
Bitmap img = BitmapFactory.decodeStream(is);
result = img;
and return result in doInBackground(). You have them switched around so 'img' will be null no matter what happens.
Also, you can't use Toast in doInBackground() as this method isn't run on the UI thread. You will need to make that a Log or put your Toast in onPostExecute() or onProgressUpdate(). These are the things I see. If you are still having problems then you need to be a little more clear on what specifically. You will need to debug and use breakpoints to see what isn't being returned that should be and pinpoint a little more what the problem is
AsyncTask - any UI updates must be done in one of the other methods of AsyncTask other than doInBackground() or you can pass a value back to the Activity to update the UI there.
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;
}