i am decoding an image from a url and i have an array of different urls,i want to show the bitmap images decoded from the url in the imageview but only the image from last url is showing.I am not able to understand the problem.
String [] imageLocation={"http://wallbase1.org/thumbs/rozne/thumb-499842.jpg","http: //ns3002439.ovh.net/thumbs/rozne/thumb-2493796.jpg","http://ns3002439.ovh.net/thumbs/rozne/thumb-2486664.jpg"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image_view = (ImageView)findViewById(R.id.imageview);
while(i<2)
{
bitmap=loadImage(imageLocation[i]);
image_view.setImageBitmap(bitmap);
Animation rotate = AnimationUtils.loadAnimation(LoadImageActivity.this, R.anim.rotate);
findViewById(R.id.imageview).startAnimation(rotate);
i++;
}
}
public Bitmap loadImage(String image_location){
URL imageURL = null;
try {
imageURL = new URL(image_location);
}
catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection connection= (HttpURLConnection)imageURL.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);//Convert to bitmap
}
catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
}
Related
I want to show an image from online (url) but it shows me nothing and i don't know why this happens. I add INTERNET permission in manifest also.
private ImageView iv;
private Bitmap bitmap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String address = "http://4.bp.blogspot.com/-3_EM3_dBstc/UxD7U2EEa2I/AAAAAAAACmI/M9JilH6eIV0/s100/Krita-logo.png";
iv = (ImageView) findViewById(R.id.image);
bitmap = bitImageRead(address);
iv.setImageBitmap(bitmap);
}
Where i do wrong?
Try this..
public Bitmap bitImageRead(address){
try {
URL url = new URL(address);
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;
}
}
On the site http://218.93.33.59:85/map/zjmap/ibikegif.asp?flag=2&id=9, we can get a gif image named ibikegif.gif. However I can't display it on my android phone. My code like this:
/**
*#param url the imageURL.it references to
*"http://218.93.33.59:85/map/zjmap/ibikegif.asp?flag=2&id=9" here
*
**/
public Bitmap returnBitMap(String url) {
URL myFileUrl = null;
Bitmap bitmap = null;
try {
myFileUrl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is); //Attention:bitmap is null
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
Could you help me?
I have tested this code and it works. It's basically what you did with one difference, i have also set request method explicitly to GET.
public class MyActivity extends Activity {
private ImageView imageView;
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.imageView);
}
#Override
protected void onResume() {
super.onResume();
new AsyncTask<Void, Void, Void>() {
Bitmap bitmap;
#Override
protected Void doInBackground(Void... params) {
try{
bitmap = returnBitMap("http://218.93.33.59:85/map/zjmap/ibikegif.asp?flag=2&id=9");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
imageView.setImageBitmap(bitmap);
imageView.invalidate();
}
}.execute();
}
public Bitmap returnBitMap(String url) throws IOException {
URL myFileUrl = null;
Bitmap bitmap = null;
InputStream is = null;
try {
myFileUrl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.setRequestMethod("GET");
conn.connect();
is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
if(is != null) {
is.close();
}
return bitmap;
}
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Android image view from url
I am trying to fetch the image from url and load in imageView, but I cannot encode the url string, please let me know how to encode this.
Please find the code & url used for your reference.
URL: http://images.pcmac.org/SiSFiles/Schools/TN/JacksonMadisonCounty/RoseHillMiddle/Uploads/Locations/%7BB690E93E-F7C9-48AF-B72A-BFF944FA6D4A%7D_104_9169.JPG
Code
String link=URLEncoder.encode(urlStr);
bitmap=loadBitmap(link);
public static Bitmap loadBitmap(String url) {
Bitmap bitmap = null;
try {
InputStream in = new java.net.URL(url).openStream();
bitmap = BitmapFactory.decodeStream(in);
} catch (Exception e) {
}
return bitmap;
}
Use this Code it will fetch the image from server
String URL = "http://images.pcmac.org/SiSFiles/Schools/TN/JacksonMadisonCounty/RoseHillMiddle/Uploads/Locations/%7BB690E93E-F7C9-48AF-B72A-BFF944FA6D4A%7D_104_9169.JPG";
private InputStream OpenHttpConnection(String urlString) throws IOException {
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
} catch (Exception ex) {
throw new IOException("Error connecting");
}
return in;
}
private Bitmap DownloadImage(String URL) {
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
final String msg = e1.getMessage();
handler.post(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "" + msg, Toast.LENGTH_SHORT).show();
}
});
}
return bitmap;
}
FYI, URLEncoder.encode(String) has been deprecated.
Reference:
http://developer.android.com/reference/java/net/URLEncoder.html
You can encode the url as below:
String URL = "http://images.pcmac.org/SiSFiles/Schools/TN/JacksonMadisonCounty/RoseHillMiddle/Uploads/Locations/%7BB690E93E-F7C9-48AF-B72A-BFF944FA6D4A%7D_104_9169.JPG";
String encodeUrl=URLEncoder.encode(URL, "utf-8");
private Bitmap DownloadImage(String URL) {
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
final String msg = e1.getMessage();
handler.post(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "" + msg, Toast.LENGTH_SHORT).show();
}
});
}
return bitmap;
}
I have create example for How to display image from URL. Use that example it works for me and i also check it by replace you image url.
Please Use below code for download and display image into imageview.
public class MainActivity extends Activity {
ImageView my_img;
Bitmap mybitmap;
ProgressDialog pd;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView mImgView1 = (ImageView) findViewById(R.id.mImgView1);
DownloadImageFromURL mDisImgFromUrl = new DownloadImageFromURL(mImgView1);
mDisImgFromUrl.execute("http://images.pcmac.org/SiSFiles/Schools/TN/JacksonMadisonCounty/RoseHillMiddle/Uploads/Locations/%7BB690E93E-F7C9-48AF-B72A-BFF944FA6D4A%7D_104_9169.JPG");
}
private class DownloadImageFromURL extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd = new ProgressDialog(MainActivity.this);
pd.setMessage("Loading...");
pd.show();
}
public DownloadImageFromURL(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
pd.dismiss();
}
}
}
I am trying to set image on imageview but image is not show.
I am reading image url from json data and then trying to set it on ImageView but my image is not visible. No any exception occur.
Here is my code
HotelList.class
static final String TAG_DISHIMAGEURL = "dishimageurl";
......
String imageUrl = dishResult.getString(TAG_DISHIMAGEURL);
map.put(TAG_DISHIMAGEURL, imageUrl);
.....
dishimageurl1 = hm.get(TAG_DISHIMAGEURL).toString();
intent.putExtra("background", dishimageurl1);
HotelDetails.class
......
String dishimageurl = bundle.getString("background");
Bitmap bimage= getBitmapFromURL(dishimageurl);
imageView.setImageBitmap(bimage);
....
public Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
Toast.makeText(this, "showing image", Toast.LENGTH_LONG).show();
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
Toast.makeText(this, "showing exception", Toast.LENGTH_LONG).show();
return null;
}
}
I don't understand what happen with this code. No any exception but my image is not visible.
Please give me any reference.
Please Use below code for get image from url and display into imageview.
public class image extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bitmap bitmap = DownloadImage("http://www.gophoto.it/view.php?i=http://1.bp.blogspot.com/-2LTvCCufBKc/T3L3KgcTj2I/AAAAAAAABbQ/Ki60e1LU9sE/s1600/Sachin%2BTendulkar.png");
RelativeLayout mRlayout1 = (RelativeLayout) findViewById(R.id.mRlayout1);
Drawable d=new BitmapDrawable(bitmap);
mRlayoutLogin.setBackgroundDrawable(d);
}
private InputStream OpenHttpConnection(String urlString) throws IOException {
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
} catch (Exception ex) {
throw new IOException("Error connecting");
}
return in;
}
private Bitmap DownloadImage(String URL) {
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return bitmap;
}
}
you can view image by using this code.
try {
bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
It seems you are downloading the image from UI thread. this will block the UI thread and will give you not responding error. as an easy way, you can use a library like Universal Image Loader
Universal Image Loader - GitHub
this will manage the image loading for you and avoid problems like incorrect urls, Out Of Memory error.
I am using the following code
public class Img extends Activity {
/** Called when the activity is first created. */
ImageView img;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img = (ImageView)findViewById(R.id.imgview);
Bitmap bm = getBitmapFromURL("http://l.yimg.com/a/i/us/we/52/21.gif");
if(bm == null)
Toast.makeText(this, "Image can't load", 1).show();
else
img.setImageBitmap(bm);
}
public static Bitmap getBitmapFromURL(String src) {
try {
Log.e("src",src);
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
Log.e("Bitmap","returned");
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
Log.e("Exception",e.getMessage());
return null;
}
}
}
I am getting a message "Image can't load"
Please try below code.
ImageView img;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img = (ImageView)findViewById(R.id.imgview);
Drawable image = getBitmapFromURL("http://l.yimg.com/a/i/us/we/52/21.gif");
if(image == null)
Toast.makeText(this, "Image can't load", 1).show();
else
img.setImageDrawable(image);
}
public Drawable getBitmapFromURL(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
return null;
}
}
try this..
private Bitmap downloadUrl(String url) {
InputStream myInputStream =null;
Bitmap myBitmap;
StringBuilder sb = new StringBuilder();
//adding some data to send along with the request to the server
sb.append("name=Anthony");
URL url;
try {
url = new URL(url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
OutputStreamWriter wr = new OutputStreamWriter(conn
.getOutputStream());
// this is were we're adding post data to the request
wr.write(sb.toString());
wr.flush();
myInputStream = conn.getInputStream();
wr.close();
myBitmap = BitmapFactory.decodeStream(myInputStream);
} catch (Exception e) {
//handle the exception !
Log.d(TAG,e.getMessage());
}
return myBitmap;
}