This is the code I'm using.
String imageURL;
Bitmap bitmap = null;
imageURL = "http://graph.facebook.com/" + fbID + "/picture?type=";
try {
bitmap = BitmapFactory.decodeStream((InputStream) new URL(imageURL)
.getContent());
} catch (Exception e) {
e.printStackTrace();
}
when i use this URL in browser it shows image, but when trying to get it in bitmap it gives null.
I've checked many question, but nothing help me.
This code works for me with HTTPGet:
HttpGet httpRequest = new HttpGet(URI.create(linkUrl) );
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
bmap = BitmapFactory.decodeStream(bufHttpEntity.getContent());
httpRequest.abort();
This problem is due to the fact that, Facebook uses its graph API url merely as a redirection endpoint. If you copy paste a facebook profile picture URL such as
http://graph.facebook.com/subinsebastien/picture
What actually happens is that, Facebook redirects you to the actual image URL. Thus, when you try to open an input stream with the graph API url, you wont actually get a bitmap, rather a redirect response is what you get. In my case, this is where I'm being redirected to.
https://scontent-b.xx.fbcdn.net/hprofile-prn1/l/t5.0-1/48771_100000333400619_475851971_q.jpg
Thus, I would rather suggest you to use an actual HTTPGet to get the bitmap first.
Related
I'm working/learning on an app for my store. The whole purpose of this app is to a customer can check recent flyer in the app.
How can I replace that image every week? how does update on app (like image change) works? Does customer need to update the app in order to see new image? or is there any way where my app will fetch photo from "XYZ" location online, and all I have to do is upload that image online to "XYZ" location.
I suggest showing your weekly image from a URL like this
ImageView img = (ImageView) findViewById(R.id.imageView1);
try {
URL url = new URL("http://web.yourcompany.com/weekly.jpg");
HttpGet httpRequest = null;
httpRequest = new HttpGet(url.toURI());
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
InputStream input = b_entity.getContent();
Bitmap bitmap = BitmapFactory.decodeStream(input);
img.setImageBitmap(bitmap);
} catch (Exception ex) {
}
or using the WebView to load the image from URL.
WebView web = (WebView) findViewById(R.id.webView1);
web.loadUrl("Your/Url.jpg");
Ideally, you want to call this code from a separate thread.
Hello I am trying to get Bitmap from URL, Here this URL is redirecting to the another https URL.
URL : http://graph.facebook.com/244054592454345/picture
I am trying to get bitmap like below but this is not working for me, tried this Android : Getting a Bitmap from a url connection (graph.facebook.com) that redirects to another url but not worked for me.
Using below code, I am always getting null in the bitmap.
URL imgUrl = new URL("http://graph.facebook.com/{user-id}/picture?type=large");
InputStream in = (InputStream) imgUrl.getContent();
Bitmap bitmap = BitmapFactory.decodeStream(in);
Any idea to get bitmap from a link which redirect to another as like me ?
This below code solved my problem and working fine.
URL fbAvatarUrl = new URL("http://graph.facebook.com/"+userProfileModel.getFacebookID()+"/picture");
HttpGet httpRequest = new HttpGet(fbAvatarUrl.toString());
DefaultHttpClient httpclient = HttpClient.getInstance();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
Bitmap fbAvatarBitmap = BitmapFactory.decodeStream(bufHttpEntity.getContent());
httpRequest.abort();
I'm having a bit of a problem with decodeStream returning null. It seems to be a fairly common problem around, but it's usually pinned down to one of two problems:
An OutOfMemory exception thrown by attempting to load a large bitmap in it's entirety.
Attempting to use the same input stream twice.
However, I'm not doing either. The code to run it is simply
stream = new java.net.URL(url).openStream();
Bitmap image = BitmapFactory.decodeStream(stream);
stream.close();
with the URL set to here. image is null after this code is complete. This issue's been driving me completely insane - it works fine on PNGs but seems to fall apart under every BMP I can give it, so any help would be appreciated.
Ultimately, the answer was found here, using an InputStream returned by a BufferedHTTPEntity. While it seems needlessly complex, I can only assume that simply getting a stream from the URL object directly doesn't return a stream of the appropriate type, and so it wasn't reading out all the data properly.
Cross-posting the code in case the question is erased:
private static InputStream fetch(String address) throws MalformedURLException,IOException {
HttpGet httpRequest = new HttpGet(URI.create(address) );
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
InputStream instream = bufHttpEntity.getContent();
return instream;
}
Here is the link where I place a REST GET call.
http://adtouch.cloudfoundry.com/rest/ad/barcode/529a927973654526a309a77986062566/image
On opening it in browser, it redirects to other link and I get an image displayed.
I want to display this image on android. I am unable to determine what is the content I get by this call and how to convert it to an image to be displayed on android device.
This is the rest call I make:
HttpClient httpclient = new DefaultHttpClient();
String temp = "http://adtouch.cloudfoundry.com/rest/ad/barcode/529a927973654526a309a77986062566/image";
HttpGet request = new HttpGet(temp);
HttpResponse response = httpclient.execute(request);
ResponseHandler<String> handler = new BasicResponseHandler();
String result = httpclient.execute(request, handler);
System.out.println(result);
The result of this is numerous sequence of characters like:
(^( ?( ;QE??QE??ÊMÔâ1MT ??²¤i#íù¨ùhÙQ¶ê??m7Í¡?mFÏ#3|µu]¿Þ§+Uò?\°Ñ³Qçý?o?MiUWt?*¯÷?ª9Jæ·û´Ý¿59_åùi¬Õq$jE¨)ÿ??Á#D°?©ËÒªÆß7Þ«?õ q?/ÉR+Tr%F?¨(·LÙQ9]¨Ê´í?½;}??5???¨ßE??º?ôúc÷ ?·éGÍíFæZ??MK·ûÔè÷?Þ©<¥Û#Æ?ýÚ±üÕEJ7l O?Z³YêÄÒï¨ÕhK{}ë¹¾íZ?>U¨c}?RG#QGðüÍM2®>\·û´??í?l¨ÿ??xß«NUþón }Ênÿ??î9UR???nÖ?Å·ýÚo?¿Ä»¿Þ©(
I know how to set an image to the ImageView but the question is how to get an image from this call.
Look at the Javadoc for BitmapFactory. You must decode the stream in the HTTP response's entity:
InputStream in = client.execute(request).getEntity().getContent();
Bitmap bmp = BitmapFactory.decode(in);
in.close();
Note that network operations should not happen in the main thread. From your code it's not clear if you use an AsyncTask or not, however you should. Update the image in the onPostExecute() hook.
On facebook, if we share some url, facebook automatically fetch an image and some initial text from the url. Any idea how to achieve this inside android app(getting image and some text from the url automatically).
Any help would be highly appreciated.
Thanks !
You can make a simple HttpRequest for the url and then filter the HTML code to get the required content.
Sample code for HttpRequest -
HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client
HttpGet httpget = new HttpGet("http://example.com"); // Set the action you want to do
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
String line = null;
while ((line = reader.readLine()) != null)
{ sb.append(line + "\n"); }
String resString = sb.toString();
is.close();
Facebook,Google+ use meta tags for the short info that is shared with the image.
For the image look for the first <img> tag and then strip of the src="" from it to get the url. Then perform a AsyncDownload from the given image url to produce a drawable -
try
{
InputStream is = (InputStream) new URL(src).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
return null;
}
You can try to use Aquery (similar to JQuery but for mobile), this library will give you lazy loading technique
OR
You can make a webservice that can return you data in the form of JSON and then you have to just parse JSON and get your image as well as text !!!
Hope this helps!!!!!