I'm trying to get a static image from a map and put on my Android layout. So, looking for it I've found a code talking about Google Static Maps. Awesome! I've tried some URLs on my PC and it's working perfectly!
But my problem was when I've put the URL in Android to test it. WHen I try to get the input stream (the image), it gives me FileNotFoundExceptioin, because when I tried to connect, it threw me a Bad Request (error 400)
Here is my code:
public class ImageLoadingTask extends AsyncTask<String, Void, Bitmap> {
#Override
protected Bitmap doInBackground(String... stringURL) {
Bitmap bmp = null;
try {
URL url = new URL(stringURL[0]);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
BitmapFactory.Options options = new BitmapFactory.Options();
bmp = BitmapFactory.decodeStream(is, null, options);
} catch (IOException e) {
e.printStackTrace();
}
return bmp;
}
#Override
protected void onPostExecute(Bitmap result) {
mapView.setImageBitmap(result);
super.onPostExecute(result);
}
}
String for test
Do I need a key for this?? Why don't I need a key for doing it in a browser?
The URL you're sending to Google has a strange character in it. Before sending it, run a URLEncode.encode() on it or if you're always showing São Paulo you could say "Sao Paulo" and let Google figure it out, or "S%C3%A3o%20Paulo". Personally I think the first one is less error prone but it's up to you.
Related
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.
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.
This question already has answers here:
How to load an ImageView by URL in Android?
(23 answers)
Closed 8 years ago.
I am making an app and I would like to keep the images updated when I change them. I am not sure how to display an image from a specific URL.
any help and suggestions is appreciated
thank you
Possibly a duplicate, as mentioned before, but still, I'd use an AsyncTask to load an image from an URL (which doesn't seems to be the case in the provided links).
Something along these lines:
new AsyncTask<String, Void, Drawable>() {
#Override
protected Drawable doInBackground(String... params) {
Drawable result = null;
try {
URL url = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream input = connection.getInputStream();
result = Drawable.createFromStream(input, "src");
} catch (MalformedURLException muExp) {
Log.e(TAG, "Bad URL provided!", muExp);
} catch (IOException ioExp) {
Log.e(TAG, "Error loading content from URL!", ioExp);
}
return result;
}
#Override
protected void onPostExecute(Drawable result) {
// Do something with your Drawable, in UI, here...
}
}.execute("http://myimageurl.com/image.jpg");
Here is how i am adding marker to map
map.addMarker(new MarkerOptions()
.position(model.getLatLongfromService())
.title(model.getCoupon_name())
.snippet(model.getCoupon_id())
.icon(BitmapDescriptorFactory.fromFile(DataHolder.imageUrl
+ model.getCoupon_image())));
I am getting coupon_image in this format : http://www.xyz.com/coupon21.jpg**
I am getting this error when u run my app.
java.lang.IllegalArgumentException: File http://test.xyz.de/uploads/company_logo/sample-logo-110x60.jpg contains a path separator
Can anyone help me to understand what the problem is ?
Thanks,
Rakesh
I think the problem is that method BitmapDescriptorFactory.fromFile uses parameter String fileName, which represents name of the file(image) to load.
You supply image's http url (http://test.xyz.de/uploads/company_logo/sample-logo-110x60.jpg) instead of it.
You probably need to download the image first and then use BitmapDescriptorFactory.fromBitmap;
EDIT:
To download image, you can use some AsyncTask like this for example:
AsyncTask<String, Void, Bitmap> loadImageTask = new AsyncTask<String, Void, Bitmap>(){
#Override
protected Bitmap doInBackground(String... params) {
Bitmap bmImg = null;
try {
URL url = new URL(params[0]);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
}
catch (IOException e)
{
e.printStackTrace();
bmImg = null;
}
return bmImg;
}
#Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
// TODO: do what you need with resulting bitmap - add marker to map
}
};
then don't forget to execute asynctask with proper parameter - String array containing url of image to download:
loadImageTask.execute(new String[]{yourImageUrl});
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.