This question already has answers here:
Load image from url
(17 answers)
Closed 9 years ago.
With Android Studio I have a code that displays images from a direct URL.
I want that displays all the images from a site (they are item called <enclosure/>) and put that in my Custom ListView. So I get the String of the URLs where is present the image and then show it by the code below but I get nothing. Can you help me?
TextView txtImage = (TextView)rowView.findViewById(R.id.item_image);
txtImage.setText(web.get(position).getEnclosure());
txtImage.setTypeface(myTypeface);
ImageView img = (ImageView) rowView.findViewById(R.id.enclosure);
try {
URL url = new URL(context.getString(R.id.item_image));
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) {
}
This is all you need to do:
URL url = new URL("http://image10.bizrate-images.com/resize?sq=60&uid=2216744464");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);
It's not recommended to do this on the UI thread, as it blocks, but you can do it in an AsyncTask.
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();
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.
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!!!!!
I need to display an image in my android application. The problem is I want to get this image from somewhere online. The URL is like:
http://www.mydomain.com/hello world image.png
As you see the image name containing some spaces in it. Every time I execute my code this will show me exception of FileNotFound. and nothing happens.
Following is my code
String imagePathCon = "hello world image.png";
String imagePath = "http://www.mydomain.com/" + imagePathCon;
try {
URL url;
url = new URL(imagePath);
// url = new URL("http://www.azuma-kinba.com/wp-content/uploads/2012/05/Android-Make-Google-Loss-in-2010.png");
InputStream content = (InputStream)url.getContent();
Drawable d = Drawable.createFromStream(content , "src");
im.setImageDrawable(d);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I use to replace space with "+" but nothing happens.
This will work.
String imagePathCon = "hello world image.png";
imagePathCon=imagePathCon.replaceAll(" ", "%20");
String imagePath = "http://www.mydomain.com/" + imagePathCon;
You must know http://en.wikipedia.org/wiki/Percent-encoding#Character_data
plese use correct Url for getting image and use below code that would definetly help u...
replace space of URL using...
imagePath=imagePath.replaceAll(" ", "%20");
and now...
HttpGet httpRequest = new HttpGet(new URL(params[0]).toURI());
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpClient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
InputStream is = bufHttpEntity.getContent();
//image_value = new URL("image Url is here");
bm = BitmapFactory.decodeStream(new FlushedInputStream(is));
//imageLoader is object of iamge view
imageLoader.setImageBitmap(bm);
You need to url encode the path, like this:
String imagePathCon = URLEncoder.encode("hello world image.png", "UTF-8");
You can also use an http get request:
The simplest way is to simply call URLEncoder which will automatically replace all charaters in your string to the url encoded format.
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 30000);
HttpConnectionParams.setSoTimeout(httpParameters, 30000);
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpGet httpGet = new HttpGet(URLEncoder.encode(url, "UTF-8"));
HttpResponse httpResponse = httpclient.execute(httpGet);
in = httpResponse.getEntity().getContent();
//Bitmap bmp = BitmapFactory.decodeStream(instream);
//create a bitmap or an image from the input stream