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;
}
Related
I have used two method to load image from remote server. It is working good in version 2.1(Samsung) and 2.3.4(Sony Ericsson Xperia Neo V). But the method is returning null in a samsung mobile V-2.2. The 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 a = realImageUrl.replace("https", "http");
InputStream is = (InputStream) this.fetch(a);
Log.e("https,SubString http: ", realImageUrl + "," + a);
Drawable d = Drawable.createFromStream(is, "saveFilename");
return d;
} catch (MalformedURLException e) {
return null;
} catch (IOException e) {
return null;
}
}
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
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;
}
}
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.