How to create map as ImageView in android? - android

I am trying to create a ImageView with help of MapView something like in pic:
Guys please give me some idea how to do this.

If you want a static map, you can just do the same as me:
http://maps.google.com/maps/api/staticmap?center=-15.800513%2C-47.91378&zoom=16&format=png&maptype=roadmap&mobile=false&markers=|color:%23128DD9|label:Marker|-15.800513%2C-47.91378&size=1000x400&key=&sensor=false
Change the parameters
?center= which will tell you where the center of the image shoud
be
label:Marker The position where the marker should appear.
To load this image to an ImageView:
public static Bitmap loadBitmap(String url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 1;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
} catch (IOException e) {
Log.e(TAG, "Could not load Bitmap from: " + url);
} finally {
closeStream(in);
closeStream(out);
}
return bitmap;
}
This method will return a Bitmap to set this Bitmap in the ImageView just do like this:
ImageView img = (ImageView)findViewById(R.id.imageView1);
Bitmap b = loadBitmap(urlToTheImage);
img.setImageBitmap(b);

I'm not exactly sure what you mean, but from the looks of it you want the Google Static Maps API.
https://developers.google.com/maps/documentation/staticmaps/
This will generate an image of a map when you give it the latitude, longitude etc. You can then use this in an ImageView as you require.
The advantage is that you don't have to create an expensive MapView, but it will not be interactive

Related

How to get String type in setImageResource method?

My code: holder.icon.setImageResource(current.imageUrl); here the imageUrl is been declared in String. But setImageResource takes only int. Can anyone provide me a solution how to get a string or is there anyother method available for it?
I think u are fetch the image from internet.
private Bitmap getBitMapFromUrl( String imageuri){
HttpURLConnection connection=null;
try {
URL url=new URL(imageuri);
connection= (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream is=connection.getInputStream();
Bitmap mybitmap=BitmapFactory.decodeStream(is);
return mybitmap;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
}}
pass the string value and return the bitmap.
holder.icon.setImageBitmap(getBitmapFromUrl());
you are using the "setImageResource" !
it expects a Resource (usually a drawable resource), hence the int requirement.
the download solution suggested by #Mayuri Joshi might fit your needs, if not, please provide more information regarding what it is you are trying to accomplish :)
You have to download the image firstly
public static Bitmap loadBitmap(String url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 1;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
} catch (IOException e) {
Log.e(TAG, "Could not load Bitmap from: " + url);
} finally {
closeStream(in);
closeStream(out);
}
return bitmap;
}
Then use the Imageview.setImageBitmap to set bitmap into the ImageView

android decode string base 64 to bitmap

Hi guys I wanted to ask you one thing, I have a chat that transfers strings and I can even attach of JPEG images before sending them to convert it into a string and then decode in BITMAP just that when I decode it crashes the app. I wanted to know if it is the right code to decode it.
NOME = (TextView) row.findViewById(R.id.comment);
NOME.setText(coment.comment);
String a = NOME.getText().toString();
if(a.length() > 1024 )
{
byte[] image = Base64.decode(a, 0);
int lung = a.length();
Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, lung);
Image = (ImageView) row.findViewById(R.id.image);
Image.setImageBitmap(bitmap);
}
The code looks fine, if I had to guess I would say you're getting the Out of Memory error, which is very common when loading images. Check out
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
for some best practices when loading images.
The method for Encoding an Image to String Base64 :
public static String encodeToString() {
String imageString = null;
try {
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
imageString = Base64.encodeToString(b, Base64.DEFAULT);
} catch (Exception e) {
e.printStackTrace();
}
return imageString;
}
The method for Decoding String Base64 to Image :
public static void decodeToImage(String imageString) {
try {
byte[] imageByte = Base64.decode(imageString, Base64.DEFAULT);
Bitmap bm = BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length);
image_view.setImageBitmap(bm);
} catch (Exception e) {
e.printStackTrace();
}
}

Android: Widget load images

I am unable to fiund how to download images to my widget!
In my widget I load in AsyncTask json with url, title, then I show title in TextView and I need to load images from url.
I tried with this but image load, and now showing
class LoadImages extends AsyncTask<Void, Void, Bitmap>{
#Override
protected Bitmap doInBackground(Void... params) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL("http://mysite/simple.img").openStream());
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 1;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
Log.e("Log", "Yeah");
} catch (IOException e) {
Log.e("Log", "Could not load Bitmap from: " + "mysiteg");
}
return bitmap;
}
}
And on Update I call this
LoadImages load = new LoadImages();
load.execute();
Bitmap bitmap = load.get();
update.setImageViewBitmap(R.id.imageView0, bitmap);
You should override onPostExecute method in LoadImages class, and set bitmap to your ImageView in that method. When LoadImages task is done, it will call onPostExcute method. Considering WeakReference to wrap your ImageView for better performance.

How to set background for an imageView from an image URL? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
how to load an imageview by url in android?
I am having an image URL.I want to set that image as my ImageViews background.
Please help..
I Hope This code Help you.
public static Bitmap loadBitmap(String url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 1;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
} catch (IOException e) {
Log.e(TAG, "Could not load Bitmap from: " + url);
} finally {
closeStream(in);
closeStream(out);
}
return bitmap;
}
Then Use imageView.setImageBitmap(bitmap);
Check this Link How to display image from URL on Android
You can then use setBackgroundDrawable() with the drawable created from the URL.

Problem with downloading image from URL

I use such code for downloading image from URL:
public static Bitmap downloadImage(String url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
} catch (IOException e) {
Log.e(TAG, "Could not load Bitmap from: " + url);
} finally {
closeStream(in);
closeStream(out);
}
return bitmap;
}
When I send URL "http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png" it works fine, but when I use "http://www.hospimedica.com/images/stories/articles/article_images/_CC/20110328%20-%20DJB146.gif" it returns me null.
What's wrong with this URL?
Why are you writing your own method to downlaod an image ? Android has inbuilt method to achieve this.. Just use
URL url = new URL("Your url");
Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());

Categories

Resources