Android - Loading picture from web no longer working - android

I have code that i was copying from another project that will allow you to pass it a url and it will return a picture...then you set it to an image view and it will display the picture...but somewhere down the line...it isn't displaying the retrieved picture anymore...can you guys look at this code and tell me if there is something wrong???
CLICK EVENT
void NextPic(View v)
{
picNum++; //int holding what pic number we are on
Drawable bPic = LoadImageFromWebOperations(picData[]); //picData = String[] with url data in it
imgView.setImageDrawable(bPic); //Cast earlier in the code
}
Get Pic Function
private Drawable LoadImageFromWebOperations(String url)
{
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}
catch(Exception e)
{
//Custom error handler
uu.SendError(e.getMessage(), "Main.GetPickTask.LoadImageFromWeboperations(String url)");
return null;
}
}
LogCat shows nothing at all...there are no error caught...and the pic keeps coming up blank...i even used the ic_launcher graphic as a placeholder so see if it was the imageview itself...but that shows up for a few seconds and then goes black
Here is an example URL i am using
http://app.guycothal.com/Funnys/2012722133515231.jpg

private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
try {
InputStream is = (InputStream) fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
return null;
} catch (IOException e) {
return null;
}
}
public Object fetch(String address) throws MalformedURLException,IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}

Related

Why I am getting OutOfMemory Exception in Android? [duplicate]

I have question about this error.
I make favicon parser from URLs. I do this like:
public class GrabIconsFromWebPage {
public static String replaceUrl(String url) {
StringBuffer sb = new StringBuffer();
Pattern p = Pattern.compile("https?://.+\\..+?\\/");
Matcher m = p.matcher(url);
while (m.find()) {
sb.append(m.group());
}
return sb.toString();
}
public static String getFavicon(String url) throws IOException {
try {
Document doc = Jsoup.connect(url).get();
Element element = doc.head().select("link[href~=.*\\.(ico|png)]").first();
if (element != null) {
if (element.attr("href").substring(0, 2).contains("//")) {
return "http:" + element.attr("href");
} else if (element.attr("href").substring(0, 4).contains("http")) {
return element.attr("href");
} else {
return replaceUrl(url) + element.attr("href");
}
} else {
return "";
}
} catch(IllegalArgumentException ex) {
ex.printStackTrace();
} catch(OutOfMemoryError er) {
er.printStackTrace();
}
return "";
}
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
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;
}
}
}
and how I get bitmap from url
Bitmap faviconBitmap = GrabIconsFromWebPage.getBitmapFromURL(
GrabIconsFromWebPage.getFavicon(
bookmarkData.get(position).getUrl() // url from which I want to grab favicon
)
);
And this code after uploading 20 images give me OutOfMemoryError. How can I fix this? Or optimize? Cuz in my list where I show this icons, can be more than 20 or 40 favicons...
I think, you would use universal image loader
The method as given snippet
// Load image, decode it to Bitmap and return Bitmap synchronously
ImageSize targetSize = new ImageSize(80, 50);
// result Bitmap will be fit to this size
Bitmap bmp = imageLoader.loadImageSync(imageUri, targetSize, options);
And for out of memory bound you would add a line in manifest file
<application
...
android:largeHeap="true"
...
>
</application>
It was bad idea with parsing icons by myself. Google did it before us
http://www.google.com/s2/favicons?domain=(domain)

Displaying an image from URL

I'm having some troubles displaying an image I am fetching from a URL into an ImageView. With the code I have at the moment, I am getting absolutely nothing. Is there a problem with this code?
Drawable img = image.LoadImageFromWeb(icon);
imageView.setImageDrawable(img);
public static Drawable LoadImageFromWeb(String iconId) {
try {
String url = "http://ddragon.leagueoflegends.com/cdn/4.3.12/img/profileicon/" + iconId + ".png";
InputStream is = (InputStream) new URL(url).getContent();
Drawable icon = Drawable.createFromStream(is, "src name");
return icon;
} catch (Exception e) {
return null;
}
}
try this function
private void downloadImage()
{
Bitmap bitmap = null;
try {
URL urlImage = new
URL("http://ddragon.leagueoflegends.com/cdn/4.3.12/img/profileicon/" + iconId +
".png");
HttpURLConnection connection = (HttpURLConnection)
urlImage.openConnection();
InputStream inputStream = connection.getInputStream();
//****bitmap is your image*****
bitmap = BitmapFactory.decodeStream(inputStream);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
and use asyncTask to wait until downloading the image like this
private class Asyn_SaveData extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
//get the random string from prefs
if (context != null)
downloadImage();
return null;
}
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
//do what you want after the image downloaded
}
}
Note: AsyncTask must be subClassed, and after the doInBackground finish its job it calls automatically the onPostExecute

How to download ONE image from remote server and display in ANDROID (CLOSED)

Currently I'm trying to download an image which is stored in my server directory. But I don't know why my app still display that image even though that image is replace with another image.
Meaning that firstly I upload image1, then I can load image1. when image1 is replace by image2, the app still show image1.
Im not sure where is my error. Does it lies in the code or other reason. Needed some help!
Below is my code:
button1.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
new download().execute();
}
});
class download extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... path) {
String outPut = null;
String s = "http://url/image/img_123.jpg";
URL myFileUrl = null;
try {
myFileUrl = new URL(s);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
int length = conn.getContentLength();
int[] bitmapData = new int[length];
byte[] bitmapData2 = new byte[length];
InputStream is = conn.getInputStream();
bm = BitmapFactory.decodeStream(is);
outPut = "success";
} catch (IOException e) {
e.printStackTrace();
}
return outPut;
}
protected void onPostExecute(String file_url) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
imageView1.setImageBitmap(bm);
}
});
}
}
UPDATE
button1.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// new download().execute();
Drawable drawable = LoadImageFromWeb("http://url/image/img_123.jpg");
imageView1.setImageDrawable(drawable);
}
});
private Drawable LoadImageFromWeb(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
Here is one example which help to download image file from URL and it store your custom location, by which you can display on screen after download
http://getablogger.blogspot.in/2008/01/android-download-image-from-server-and.html
Update:
I think you need to load image direcly from url..see here
http://progrnotes.blogspot.in/2010/09/url.html

Parse json link to get the image

i am doing a project in which i have to get the image from the web server, the data is stored in json format. i have a json url, i have to get the names and image url's from the server and then get the bitmap images from the server.
Can anyone please help me in doing this.
Thank you.
use this code for getting image drawable form the url
public Object fetch(String address) throws MalformedURLException, IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
private Drawable ImageOperations(Context ctx, String url) {
try {
InputStream is = (InputStream) this.fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
return null;
} catch (IOException e) {
return null;
}
}

Load image formURL using a Thread

How should I load an image from http using a Thread in android?
I would like to show the layout while waiting the image to load.
Now I'm using this code to show my img from http, and it show a dark blank screen for a few time before show the result:
private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
try {
InputStream is = (InputStream) this.fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
return null;
} catch (IOException e) {
return null;
}
}
public Object fetch(String address) throws MalformedURLException,IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
Can anyone help me?
Use this for load images from http
ImageView img_item;
String image_url = "here url"
new Thread(new Runnable()
{
public void run()
{
try
{
final Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(image_url).getContent());
img_item.post(new Runnable()
{
public void run()
{
if(bitmap !=null)
{
img_item.setImageBitmap(bitmap);
}
}
});
} catch (Exception e)
{
// TODO: handle exception
}
}
}).start();
you should pass your ImageView object to your method. and method run as a thread.

Categories

Resources