Load image from Google Places in Android - android

I'm having trouble with loading an image from a URL into my Android app using the Google Places API.
I got the part with the API working and I can see the image if I type the URL in my browser. The code fails in the below line:
Bitmap myBitmap = BitmapFactory.decodeStream(input);
The strange thing is, it simply jumps to the return null in the catch clause without executing the println commands.
I should also explain that I'm using two URL's since the first URL in the google API is a redirect page.
I assume that the problem is that the final page that's being loaded isn't actually a "real" image (I mean it looks like
https://lh4.googleusercontent.com/XXX/s1600-w400/
rather than something like
http://something/image.jpg)
Below is the entire method I'm using:
public Bitmap getImageData(Place p) {
try {
String src= "https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference="+
p.getPhoto_reference()+
"&key="+
this.context.getResources().getString(places_api);
Log.d("srcsrc", src);
URL url = new URL(src);
HttpsURLConnection ucon = (HttpsURLConnection) url.openConnection();
ucon.setInstanceFollowRedirects(false);
URL secondURL = new URL(ucon.getHeaderField("Location"));
HttpsURLConnection connection = (HttpsURLConnection) secondURL.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Bitmap exception" + e);
return null;
}
}
Appreciate your help.

use Picasso like this :
Picasso.with(context).load(src).into(imageView );
It will do the following:
Handling ImageView recycling and download cancelation in an adapter.
Complex image transformations with minimal memory use.
Automatic memory and disk caching.

Related

How can I load an image from URL without extension android

I want to load an image from a URL but it doesn't work because the link doesn't have an extension
Can this be solved???
URL example :
http://t0.gstatic.com/images?q=tbn:ANd9GcRf1EqE2kyW12HSb9gZZ8eTIPqNgVkjFis4GkTTYONIpoQtkIde4zybZ4iAqGlIHQ_pnEX499Oa
How can this be done??
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img = (ImageView)findViewById(R.id.imageView1);
Bitmap bitImg = getBitmapFromURL("http://t0.gstatic.com/images?q=tbn:ANd9GcRf1EqE2kyW12HSb9gZZ8eTIPqNgVkjFis4GkTTYONIpoQtkIde4zybZ4iAqGlIHQ_pnEX499Oa");
img.setImageBitmap(bitImg);
}
public Bitmap getBitmapFromURL(String imageUrl) {
try {
URL url = new URL(imageUrl);
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;
}
}
look at this post, i dont see his URL but i think Android libraries figure it out based on the data and headers:
BitmapFactory.decodeStream returning null when options are set
also, don't add an extension to a URL, it changes the URL!
http://www.google.com is not the same as http://www.google.com.html
load an image from a URL but it doesn't work because the link doesn't have an extension. Nonsense. Url.openconnection does not look at extensions. It does not work because you have a NetworkOnMainThreadException clearly visible in the LogCat. Never seen in the LogCat? Please go and look for it. Place your network code in an AsyncTask or thread to prevent this.

cookies with getBitmapFromURL()

I am passing the url to get the images from the server e.g.
getBitmapFromURL("http://abc.xyz.in/logo.jpg");
But it is not returning anything. Saying that images are private. So my question is that is there anyway to pass the cookie to getBitmapFromURL() method. So that i can get the images. Or any other alternative is there? Thanks a lot.
2014, if the answer is feasible to you please raise the answer and points
This should do the trick:
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;
}
}
Don't forget to add the internet permission and any other permission which is needed in your manifest.
I got my answer. I can do this by using following line:
connection.addRequestProperty("key", value);

Image XML Parsing using sax in android

I want to parse the images in the xml file. i am able to parse the text but how to parse the images and display image corresponding to that text in list view.Image and Text should be in one row. My Xml file sample is below
<tv>
<channel id="0240.TEN ACTION.in">
<display-name>0: TEN ACTION+</display-name>
<thumb_url>http://localhost/rg.jpeg</thumb_url>
</channel>
<channel id="2130.ENTER 10.in">
<display-name>0: ENTER 10</display-name>
<thumb_url>http://localhost/rg.jpeg</thumb_url>
</channel>
</tv>
Try following code:
First you need to get the image path from xml like any other attribute of xml and then pass that URL to this function.
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;
}
}
Nothing too complicated – simply create a URL object using the “src” string (i.e. String src = “http://thinkandroid.wordpress.com”), connect to it, and use Android’s BitmapFactory class to decode the input stream. The result should be your desired Bitmap object!

Getting MalformedURLException: Protocol not found?

I am getting MalformedURLException: Protocol not found, when I am trying to load image from URL sting. That image is displaying in Browser when I paste that URL. But not displaying in ImageView. I've used this code:
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;
}
}
Please anybody tell me whats the problem? It will be very helpful to me.
Its a
http://74.208.68.90/webservice/images/image44.png
Your code is OK.
malformedexception comes when there is some problem in your url
try to encode your parameters in url with url encoder and protocall not found shows
http is missing from your url
String url = url +"/" + UrlEncoder.encode(param);
I think it is "src" that may have problem,

Pulling images from Web

I'm using the following code to grab images from the web. It uses Gridview and depending on position, picks a URL from an array. It works but the loading of images is often hit or miss. Almost every time I start the app, a different number of images load.
It also has issues when changing from portrait to landscape view, 5 out of 10 images may be displayed then I'll turn the device and usually lose all the images. Sometimes a few do show up though.
Any ideas on making this more robust?
try {
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
return bm;
} catch (IOException e) {
Log.d("DEBUGTAG", "error...");
}
return null;
One thing I read is that there's a known bug with decoding Bitmaps from an InputStream, and the suggested fix from Google was to use a FlushedInputStream (example in below URL):
http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html
Also, I'd put the download code into an AsyncTask. Here's what I currently use for mine:
public static Bitmap loadImageFromUri(URI uri)
{
URL url;
try {
url = uri.toURL();
} catch (MalformedURLException e) {
Log.v("URL Exception", "MalformedURLException");
return null;
}
try
{
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
return BitmapFactory.decodeStream(new FlushedInputStream(input));
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
}
I just pass in a URI due to the way I've got the rest of my code set up, you could pass in a URL instead and skip the first part. This will keep the download from tying up your UI thread.

Categories

Resources