I used the Below Three Methods to retrieve image But Not Working
//Function First Method
public static Drawable loadImageFromURL(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent(); // While Debugging After this line Error occurs
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
Log.i("Image", "No Image");
return null;
}
}
//Second Method
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(); // While Debugging After this line Error occurs
Bitmap MyBitmap = BitmapFactory.decodeStream(input);
return MyBitmap;
} catch (Exception e) {
e.printStackTrace();
Log.i("Image", "No Image");
return null;
}
}
//Async Task even tried this
private class ImageDownload extends AsyncTask<String, String, Drawable>{
@Override
protected Drawable doInBackground(String... str) {
Drawable image = loadImageFromURL(str[0]);
return image;
}
}
I used the above 3 methods for 1st two methods i get the below error and no image view
My error
01-09 18:08:15.294: I/Image(1221): Image Null
01-09 18:08:15.315: W/System.err(1221): android.os.NetworkOnMainThreadException
01-09 18:08:15.315: W/System.err(1221):at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
01-09 18:08:15.315: W/System.err(1221): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
01-09 18:08:15.315: W/System.err(1221): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
01-09 18:08:15.324: W/System.err(1221): at java.net.InetAddress.getAllByName(InetAddress.java:214)
01-09 18:08:15.324: W/System.err(1221): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
01-09 18:08:15.324: W/System.err(1221): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
01-09 18:08:15.324: W/System.err(1221): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
01-09 18:08:15.334: W/System.err(1221): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
01-09 18:08:15.334: W/System.err(1221): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
01-09 18:08:15.345: W/System.err(1221): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
01-09 18:08:15.345: W/System.err(1221): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
01-09 18:08:15.345: W/System.err(1221): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
01-09 18:08:15.354: W/System.err(1221): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
01-09 18:08:15.364: W/System.err(1221): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
01-09 18:08:15.364: W/System.err(1221): at in.datumdata.weather.MainActivity.getBitmapFromURL(MainActivity.java:109)
01-09 18:08:15.364: W/System.err(1221): at in.datumdata.weather.MainActivity$JSONParse.onPostExecute(MainActivity.java:187)
01-09 18:08:15.374: W/System.err(1221): at in.datumdata.weather.MainActivity$JSONParse.onPostExecute(MainActivity.java:1)
01-09 18:08:15.374: W/System.err(1221): at android.os.AsyncTask.finish(AsyncTask.java:631)
01-09 18:08:15.374: W/System.err(1221): at android.os.AsyncTask.access$600(AsyncTask.java:177)
01-09 18:08:15.384: W/System.err(1221): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
01-09 18:08:15.384: W/System.err(1221): at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 18:08:15.384: W/System.err(1221): at android.os.Looper.loop(Looper.java:137)
01-09 18:08:15.397: W/System.err(1221): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-09 18:08:15.404: W/System.err(1221): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 18:08:15.404: W/System.err(1221): at java.lang.reflect.Method.invoke(Method.java:511)
01-09 18:08:15.414: W/System.err(1221): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-09 18:08:15.414: W/System.err(1221): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-09 18:08:15.414: W/System.err(1221): at dalvik.system.NativeStart.main(Native Method)
I Want to retrieve two images from tow different urls. Help me to solve this issue Thanks in advance
you could also try this one
private class DecodeImages extends AsyncTask<String, Void, Void> { //Converter is class name
protected String doInBackground(String... urls) {
//THIS METHOD WILL BE CALLED AFTER ONPREEXECUTE
//YOUR NETWORK OPERATION HERE
return null;
}
protected void onPreExecute() {
super.onPreExecute();
//THIS METHOD WILL BE CALLED FIRST
//DO OPERATION LIKE SHOWING PROGRESS DIALOG PRIOR TO BEGIN NETWORK OPERATION
}
protected void onPostExecute(String result) {
super.onPostExecute();
//TNIS METHOD WILL BE CALLED AT LAST AFTER DOINBACKGROUND
//DO OPERATION LIKE UPDATING UI HERE
}
}
and you can simple call this class by writing
new DecodeImages().execute(new String[]{//YOUR LINK});