从Android网址获取两个或更多图片

时间:2014-01-09 13:41:40

标签: android android-asynctask imageview android-image

我使用了以下三种方法来检索图像但不工作

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

}

我在前两种方法中使用了上述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)

我想从两个不同的网址中检索两张图片。帮我解决这个问题提前谢谢

2 个答案:

答案 0 :(得分:0)

错误NetworkOnMainThreadException表示您尝试在主线程上加载图像。

如果使用aSyncTask加载图像,则不会发生此错误。

编辑:我看到你已经尝试过了一个同步任务。那么它不应该给你主要的例外网络。如果使用aSyncTask,是否会出现另外的异常?你怎么称呼aSyncTask?

答案 1 :(得分:-2)

你也可以尝试这个

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

您可以通过编写

来简单地调用此类
new DecodeImages().execute(new String[]{//YOUR LINK});