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;
}
}
Related
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)
I have created an android application where image is loading from given url.When I am passing url directly, the image is loading. But image is not loading when I am taking the url inside a string variable and passing that variable . My code is given below .
private Drawable ImageOperations(String url, String saveFilename) {
try {
String realImageUrl=url+"? email="+Constants.email+"&proc_date="+Constants.proc_date+"&access_key="
+Constants.ACCESS_KEY+"&version=1.00";
String newUrl=realImageUrl.replace("https", "http");
InputStream is = (InputStream) this.fetch(newUrl);
Log.e("https,SubString http: ",realImageUrl+","+ a);
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;
}
This code is not working. my new url is newUrl. when I am printing newUrl in my log and giving that url directly instead of newUrl the image is loading.
I have found the solution. I have updated the fetch(String address) method.
public Object fetch(String address) throws MalformedURLException,
IOException {
try{
URL url = new URL(address);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
url = uri.toURL();
Log.i("Url:", url+"");
Object content = url.getContent();
return content;
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
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;
}
I need to download one file from an url that i give from an xml after connect to other url. My problem is that i need the same user agent and ip to do both actions.
To obtain the xml, i use a PHP code that is hosted on my server and i send it the user agent of my app:
String ua=new WebView(ct).getSettings().getUserAgentString().trim();
ua = ua.replaceAll(" ", "%20");
Then i parse the xml with a generic RssParserSax that gives me all i need.
After, i try to download the file to a drawable var and i do this:
Drawable dd = ImageOperations(url);
private Drawable ImageOperations(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;
}
}
private Object fetch(String address) throws MalformedURLException,IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
But as i don't send the user agent, it does not give me anything.
Finally i did this to resolve:
Bitmap bmImg;
try {
URL url = new URL(addres);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.addRequestProperty("User-Agent", ua);
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
imagen.setImageBitmap(bmImg);
imagen.setScaleType(ScaleType.FIT_XY);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
You can use AndroidHttpClient.newInstance(useragent).
See an example here AndroidHttpClient can not getEntity().getContent() after closed
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.