2014-01-09 29 views
0

我用下面的三種方法來獲取圖像,但不工作從URL獲得兩個或多個圖像的Android

//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; 
    } 

} 

我用1兩種方法,我得到下面的錯誤和沒有圖像視圖

上述3種方法

我的錯誤

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) 

我想要從絲束不同的URL兩個圖像。幫我解決這個問題提前致謝

回答

-2

,你也可以通過編寫

new DecodeImages().execute(new String[]{//YOUR LINK}); 
試試這個

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 
    } 
} 

,你可以簡單的調用這個類

0

錯誤NetworkOnMainThreadException表示您嘗試在主線程上加載圖像。

如果使用aSyncTask加載圖像,則不會發生此錯誤。

編輯:我看到你已經嘗試過aSyncTask。那麼它不應該給你主要的例外網絡。如果使用aSyncTask,你會得到不同的異常嗎?你怎麼稱呼aSyncTask?