Android -imageview showing old images download from URL - android

this is the code I'm using.
link contain image
http://mycapturetest.byethost17.com/Screen.jpg
when i update image on URl and open it on browser. it shows updated image.
but when open the same link in android application it keep showing the last image uploaded on the URl. (Not the new one)
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
bmImage.setImageDrawable(null);
bmImage.setImageBitmap(null);
}
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.setImageDrawable(null);
bmImage.setImageBitmap(null);
bmImage.setImageBitmap(result);
Toast.makeText(MainActivity.this, "set....", Toast.LENGTH_SHORT).show();
}
}

You can turn off caching of resources loaded from URLs using the URLConnection class:
try {
URLConnection connection = new java.net.URL(urldisplay).openConnection();
connection.setUseCaches(false);
InputStream in = connection.getInputStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}

Related

How can I change an ImageView widget using a gif image online in android?

I want to get some images (which are in .gif format) from a web page and then change an ImageView widget in my app.
I found the following code on stackoverflow:
public static Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
return null;
}
}
Then, in the onPostExecute method, I have added the following line which doesn't work (although it doesn't give any visible error).
((ImageView) findViewById(R.id.imgWeather)).setImageDrawable(LoadImageFromWebOperations(pic_image)); //pic_image is the url which ends in .gif
Try this,
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(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);
}
}
and call it as,
new DownloadImageTask("//(ImageView) findViewById(R.id.imgWeather)")
.execute("//pic_image is the url which ends in .gif");

Load image larger than 2048x2048 into ImageView on Android

I need to load high res remote images to imageview, but without using any external libraries or frameworks. Images are like 2150x1500 px, and here is my code for loading:
URL imageUrl = new URL(url);
HttpURLConnection connection;
if (url.startsWith("https://")) {
connection = (HttpURLConnection) imageUrl.openConnection();
} else {
connection = (HttpURLConnection) imageUrl.openConnection();
}
connection.setConnectTimeout(30000);
connection.setReadTimeout(30000);
connection.setInstanceFollowRedirects(true);
InputStream is = connection.getInputStream();
OutputStream os = new FileOutputStream(f);
CacheUtils.CopyStream(is, os);
os.close();
image = decodeFile(f);
img.setImageBitmap(image);
and here is decodeFile function:
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
and I always get texture size too big exception. Is there any way to load these images? Or is there a way to resize image not 2 or 4 times, but resize to fit 2048 pixels by width?
class DownloadImage extends AsyncTask<String, Void, Bitmap>
{
ImageView bmImage;
public DownloadImage(ImageView bmImage) {
this.bmImage = bmImage;
}
#Override
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;
}
#Override
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(Bitmap.createScaledBitmap(result, 120, 120, false));
}
}
Specify the height and width you need instead of 120.
Call this in your oncreate
new DownloadImage((ImageView) findViewById(R.id.imageView1))
.execute("https://.........");

Universal Image Loader with PhotoView : zoom crashes

When I zoom out an image too much, the app crashes.
LogCat:
*FATAL EXCEPTION MAIN:
java.lang.IllegalArgumentException: pointerIndex out of range
at android.view.MotionEvent.nativeGetAxisValue(Native Method)
at android.view.MotionEvent.getX(MotionEvent.java.2153)*
What I have to do???
If you are using ViewPager, then replace it with HackyViewPager.
If you have a problem in that case you can use bitmap rather than universal image loader.
Hope it helps
ImageView bmImage;
public DisplayImageFromURL(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();
}

unable to fetch image from url in android [duplicate]

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

JSON to retrieve image

I am using JSON to retrieve user data and I am also getting the image URL but how can I show the image instead of the URL? Currently it only shows the URL from the image.
Here is the code:
StringBuilder tweetResultBuilder = new StringBuilder();
try {
//get JSONObject from result
JSONObject resultObject = new JSONObject(result);
//get JSONArray contained within the JSONObject retrieved - "results"
JSONArray tweetArray = resultObject.getJSONArray("results");
//loop through each item in the tweet array
for (int t=0; t<tweetArray.length(); t++) {
//each item is a JSONObject
JSONObject tweetObject = tweetArray.getJSONObject(t);
//get the username and text content for each tweet
tweetResultBuilder.append(tweetObject.getString("from_user")+": ");
tweetResultBuilder.append(tweetObject.get("profile_image_url")+"\n");
tweetResultBuilder.append(tweetObject.get("text")+"\n\n");
}
}
You should download and cashe you image first.
Please read this article Displaying Bitmaps Efficiently, and check Sample application
The shorter way to display image from url:
new DownloadImageTask((ImageView) findViewById(R.id.imageView1))
.execute("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png");
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(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);
}
}
You need to download image from the url you are getting.
For that you need to do following things.
ImageView my_image = (ImageView) findViewById(R.id.my_imageView);
String image_url = "http://www.clker.com/cliparts/1/d/7/e/12570918991185627521Ramiras_Earth_small_icon.svg.med.png";
Bitmap bm = getImageFromServer(image_url);
my_image.setImageBitmap(bm);
This will call the following function to download image.
private Bitmap getImageFromServer(String image_url) {
Bitmap bm = null;
try {
URL aURL = new URL(image_url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (IOException e) {
Log.e("DEBUGTAG", "Remtoe Image Exception", e);
}
return bm;
}
That's it. Hope this helps you somehow.
Thanks.
try this one
try {
ImageView i = (ImageView)findViewById(R.id.image);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
i.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

Categories

Resources