Unable to add icon to Marker , Map V2 Android - android

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});

Related

how can send a json frame to mysever hear i can reserve a json and i wnd send the data

I posted the code I used for reading the data from my server but I don't know how to send a json frame to the server.
I want to send the data string.
try {
URL url = new URL(params[0]);
con = (HttpURLConnection)url.openConnection();
con.connect();
InputStream in = con.getInputStream();
red = new BufferedReader(new InputStreamReader(in));
String line = "";
buffer = new StringBuffer();
while ((line=red.readLine())!= null){
buffer.append(line);
}
return buffer.toString();
I would assume that you are trying to post some JSON Object to a URL,
While using HttpURLConnection for connection you can set to its instance that this is a POST header request, if you are indeed posting something on that URL.
After that you can use DataOutputStream instance to write(POST) your JSON data something like this.
I have written a snippet which you can check, the code is also available on Github
private class MyTask extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
try {
/**
* Kindly change this url string with your own, where you want to post your json data
*/
URL url = new URL("");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
// when you are posting do make sure you assign appropriate header
// In this case POST.
httpURLConnection.setRequestMethod("POST");
httpURLConnection.connect();
// like this you can create your JOSN object which you want to send
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("email", "dddd#gmail.com"); //dummy data
jsonObject.addProperty("password", "password");// dummy data
// And this is how you will write to the URL
DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
wr.writeBytes(jsonObject.toString());
wr.flush();
wr.close();
Log.d("TAG", "" + IOUtils.toString(httpURLConnection.getInputStream()));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
This code uses apache-common-io, to get the String from the input stream, if you would like you can change that.

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.

Bad Request (Error 400) getting static map in Android

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.

how do i display an image from a certain URL in my android app? [duplicate]

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");

How to display an image from an URL within textView

I have a textView. In my code I am adding some lines of text in it. I also want to display some image from an external URL (not from my resource folder) just in between those lines. Every thing is dynamic i.e. the text generated and the image URL will be generated on the flow.So i have to fetch the image through my code and add it.
Wondering if there is a way to insert images from external URL within text view? Also any better approach is always welcome.
You will have to use this along with asynctask,
open connection in doInbackground()
set image to textview in onPostExecute()
try {
/* Open a new URL and get the InputStream to load data from it. */
URL aURL = new URL("ur Image URL");
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
/* Buffered is always good for a performance plus. */
BufferedInputStream bis = new BufferedInputStream(is);
/* Decode url-data to a bitmap. */
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
Drawable d =new BitmapDrawable(bm);
d.setId("1");
textview.setCompoundDrawablesWithIntrinsicBounds(0,0,1,0);// wherever u want the image relative to textview
} catch (IOException e) {
Log.e("DEBUGTAG", "Remote Image Exception", e);
}
hope it helps
You are going to probably want to use an asynctask to grab the image. This will run in the background from your other tasks. Your code may look something like this:
public class ImageDownloader extends AsyncTask<String, Integer, Bitmap>{
private String url;
private final WeakReference<ImageView> imageViewReference;
//a reference to your imageview that you are going to load the image to
public ImageDownloader(ImageView imageView) {
imageViewReference = new WeakReference<ImageView>(imageView);
}
#Override
protected Bitmap doInBackground(String... arg0) {
if(isCancelled())
return null;
Bitmap retVal;
url = arg0[0];//this is the url for the desired image
...download your image here using httpclient or another networking protocol..
return retVal;
}
#Override
protected void onPostExecute(Bitmap result) {
if (isCancelled()) {
result = null;
return;
}
ImageView imageView = imageViewReference.get();
imageView.setImageBitmap(result);
}
#Override
protected void onPreExecute() {
...do any preloading you might need, loading animation, etc...
}

Categories

Resources