来自Url的Android图片不起作用

时间:2014-01-09 12:47:15

标签: json android-asynctask imageview drawable bitmapfactory

我有一个JSON文件想要从中检索url并显示它的图像视图。我使用JSON PARSER ASYNC TASK在执行后执行任务以显示无效的图像视图

我的Java代码

    @Override
    protected JSONObject doInBackground(String... args) {
        JSONParser jParser = new JSONParser();

        // Getting JSON from URL
        JSONObject json = jParser.getJSONFromUrl(url);
        return json;
    }
     @Override
     protected void onPostExecute(JSONObject json) {
        try {
                // Getting JSON Array

                // Storing  JSON item in a Variable
                Url = http://icons.iconarchive.com/icons/benjigarner/softdimension/48/
                String icon = json.getString("icon"); //URL String
                String ImageUrl = Url + Icon;

                //Function to get image from URL - First method
                Drawable MyImage = loadloadImageFromURL(ImageUrl);

                //Function to get image from URL - Second method
                Bitmap MyImage = getBitmapFromURL(ImageUrl);
                //Third Method
                new ImageDownload().execute(ImageUrl);

                ImageView Image = (ImageView) rootView.findViewById(R.id.imgMainWeather);   Image.setImageBitmap(MainActivity.WeatherImage);

                } catch (JSONException e) {
            e.printStackTrace();
        }
     }
    //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)

我想从json文件中恢复3张图片 帮我解决这个问题提前谢谢

1 个答案:

答案 0 :(得分:0)

AsyncTask - 执行长时间运行的任务,防止ui被这个长时间运行的进程阻塞。

doInBackground - 执行无法在UI中进行任何更改的后台工作的方法

OnPostExecute - 在UI中进行任何更改的方法

网络OnMainThread异常:

  
    

您正在获取onPostExecute中的图片,该图片应该位于doInBackground

  

从doInBackground传递图像到onPostExecute或将下载的位图保存在静态位图变量中,该变量可以在onPostExecute中使用并使用该位图。