2

在我的應用我得到android.os.NetworkOnMainThreadException。 我想在那個時候得到的圖像的URL我得到這個Exception。 如果我在2.2上運行的應用程序它工作正常,但如果我在4.0上運行我得到異常。android.os.NetworkOnMainThreadException

public static Bitmap getBitmap(String url) 
{ 
    Bitmap bitmap = null; 

    try 
    { 
     // replace space with url encoded character. 
     url = url.replace(" ", "%20"); 
     // System.out.println("url : " + url); 
     URL urll = new URL(url); 
     InputStream in = (InputStream) urll.getContent(); 
     bitmap = BitmapFactory.decodeStream(in); 
    } 
    catch (MalformedURLException e) 
    { 
     e.printStackTrace(); 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 

    if (bitmap == null) 
     bitmap = BitmapFactory.decodeResource(resources, R.drawable.noimage); 

    return bitmap; 
} 

我在這一行越來越錯誤

InputStream in = (InputStream) urll.getContent(); 

請幫我解決這個問題

我用的AsyncTask

private class RecipeDetail1Task extends AsyncTask<Long, Void, RecipeDetailBean> 
{ 
    @Override 
    protected void onPreExecute() 
    { 
     Log.i(TAG, "<<<<<<<<<<<<<onPreExecute>>>>>>>>>>>>>>>>>>>"); 

     btnViewRecipe.setEnabled(false); 
     imgFree.setVisibility(View.GONE); 
    } 
    @Override 
    protected RecipeDetailBean doInBackground(Long... params) 
    { 
     Log.i(TAG, "<<<<<<<<<<<<<doInBackground>>>>>>>>>>>>>>>>>>>"); 

     RecipeDetailBean bean = null; 
     try 
     { 
      ParserUtil pu = new ParserUtil(ParserUtil.ResultControl.LIMITED); 
      bean = pu.getRecipeDetail(params[0]);    
     } 
     catch (ParserConfigurationException e) 
     { 
      e.printStackTrace(); 
     } 
     catch (SAXException e) 
     { 
      e.printStackTrace(); 
     } 

     return bean; 
    } 

    protected void onPostExecute(RecipeDetailBean result) 
    { 
     Log.i(TAG, "<<<<<<<<<<<<<onPostExecute>>>>>>>>>>>>>>>>>>>"); 



     Constant.recipeDetail = result; 
     if (result == null) 
     { 
      Log.i(TAG, "<<<<<<<<<<<<<result == null>>>>>>>>>>>>>>>>>>>"); 
      toast.setText(getString(R.string.recipe_detail_not_present_message)); 
      toast.setDuration(Toast.LENGTH_LONG); 
      toast.show(); 
      Constant.recipeDetail = new RecipeDetailBean(); 
      btnViewRecipe.setEnabled(false); 
     } 
     else 
     { 
      Log.i(TAG, "<<<<<<<<<<<<<result |= null>>>>>>>>>>>>>>>>>>>"); 
      //pb.setVisibility(View.INVISIBLE); 
      btnViewRecipe.setEnabled(true); 
      lblRecipeName.setText(result.getName()); 
      lblRecipeDesc.setText(result.getDescription()); 
      lblRecipeBy.setText(result.getAuthor()); 
      lblPostedOn.setText(result.getCreatedDate()); 

      String d=Integer.toString(result.getServedNumber()); 
      if(d==null) 
      { 
       lblServesNumber.setText("N/A"); 
       lblServesNumber1.setText(""); 
      } 

      else if(result.getServedNumber()==0) 
      { 
       lblServesNumber.setText("N/A"); 
       lblServesNumber1.setText(""); 
      } 
      else 
      { 
       lblServesNumber.setText(d); 
       lblServesNumber1.setText(" Adult(s)"); 
      } 
      if(Constant.recipeDetail.getFreeYN() == 1) 
       imgFree.setVisibility(View.VISIBLE); 

      //rbRecipeRating.setProgress((int) result.getRating()); 
      rbRecipeRating.setRating((float) ((result.getRating() * 5)/100)); 
      //Log.d("rating2", "" + (float) ((result.getRating() * 5)/100.0f)); 
      result.setRecipeBitmap(Constant.getBitmap(result.getImageURL())); 
      imgRecipe.setImageBitmap(result.getRecipeBitmap()); 
      imgRecipe.setVisibility(View.VISIBLE); 
      //ImageLoader imageLoader = new ImageLoader(imgRecipe, pb, null); 
      //imageLoader.execute(result.getImageURL()); 
     } 

     btnViewRecipe.requestFocus(); 
    } 
} 

我越來越此行錯誤:

result.setRecipeBitmap(Constant.getBitmap(result.getImageURL())); 

的logcat:

09-27 13:14:11.445: E/AndroidRuntime(1014): FATAL EXCEPTION: main 
09-27 13:14:11.445: E/AndroidRuntime(1014): android.os.NetworkOnMainThreadException 
09-27 13:14:11.445: E/AndroidRuntime(1014):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084) 
09-27 13:14:11.445: E/AndroidRuntime(1014):  at java.net.InetAddress.lookupHostByName(InetAddress.java:391) 
09-27 13:14:11.445: E/AndroidRuntime(1014):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242) 
09-27 13:14:11.445: E/AndroidRuntime(1014):  at java.net.InetAddress.getAllByName(InetAddress.java:220) 
09-27 13:14:11.445: E/AndroidRuntime(1014):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71) 
09-27 13:14:11.445: E/AndroidRuntime(1014):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50) 
09-27 13:14:11.445: E/AndroidRuntime(1014):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351) 
09-27 13:14:11.445: E/AndroidRuntime(1014):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86) 
09-27 13:14:11.445: E/AndroidRuntime(1014):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 
09-27 13:14:11.445: E/AndroidRuntime(1014):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308) 
09-27 13:14:11.445: E/AndroidRuntime(1014):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:303) 
09-27 13:14:11.445: E/AndroidRuntime(1014):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282) 
09-27 13:14:11.445: E/AndroidRuntime(1014):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232) 
09-27 13:14:11.445: E/AndroidRuntime(1014):  at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273) 
09-27 13:14:11.445: E/AndroidRuntime(1014):  at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168) 
09-27 13:14:11.445: E/AndroidRuntime(1014):  at com.indianrecipes.beans.Constant.getBitmap(Constant.java:239) 
09-27 13:14:11.445: E/AndroidRuntime(1014):  at com.indianrecipes.activities.RecipeDetail1Activity$RecipeDetail1Task.onPostExecute(RecipeDetail1Activity.java:376) 
09-27 13:14:11.445: E/AndroidRuntime(1014):  at com.indianrecipes.activities.RecipeDetail1Activity$RecipeDetail1Task.onPostExecute(RecipeDetail1Activity.java:1) 
09-27 13:14:11.445: E/AndroidRuntime(1014):  at android.os.AsyncTask.finish(AsyncTask.java:602) 
09-27 13:14:11.445: E/AndroidRuntime(1014):  at android.os.AsyncTask.access$600(AsyncTask.java:156) 
09-27 13:14:11.445: E/AndroidRuntime(1014):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615) 
09-27 13:14:11.445: E/AndroidRuntime(1014):  at android.os.Handler.dispatchMessage(Handler.java:99) 
09-27 13:14:11.445: E/AndroidRuntime(1014):  at android.os.Looper.loop(Looper.java:137) 
09-27 13:14:11.445: E/AndroidRuntime(1014):  at android.app.ActivityThread.main(ActivityThread.java:4340) 
09-27 13:14:11.445: E/AndroidRuntime(1014):  at java.lang.reflect.Method.invokeNative(Native Method) 
09-27 13:14:11.445: E/AndroidRuntime(1014):  at java.lang.reflect.Method.invoke(Method.java:511) 
09-27 13:14:11.445: E/AndroidRuntime(1014):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
09-27 13:14:11.445: E/AndroidRuntime(1014):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
09-27 13:14:11.445: E/AndroidRuntime(1014):  at dalvik.system.NativeStart.main(Native Method) 

回答

0

也越來越像應以某種方式在doInBackground method..as做這樣做在onPostExecute就像沒有在後臺做在主線程..

0

試試這個代碼的01

URL urll = new URL(url); 
InputStream in = (InputStream) urll.openConnection().getInputStream(); 
bitmap = BitmapFactory.decodeStream(in); 
+0

我試過的代碼,但我對你有附加的logcat錯誤 – user1083266

+0

更新得到同樣的異常。 – Pratik

16

onPostExecuteonPreExecute方法運行到應用程序的main threaddoInBackground方法運行到另一個線程,這意味着你應該下載你的圖像到doInBackground方法。

例如,如果你想從某些URL下載圖像,然後把該圖像爲ImageView

電話:

new DownloadImageTask(yourImageView).execute(yourURL);

其中的AsyncTask類:

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 

    private ProgressDialog mDialog; 
    private ImageView bmImage; 

    public DownloadImageTask(ImageView bmImage) { 
     this.bmImage = bmImage; 
    } 

    protected void onPreExecute() { 

     mDialog = ProgressDialog.show(ChartActivity.this,"Please wait...", "Retrieving data ...", true); 
    } 

    protected Bitmap doInBackground(String... urls) { 
     String urldisplay = urls[0]; 
     Bitmap mIcon11 = null; 
     try { 
      InputStream in = new java.net.URL(urldisplay).openStream(); 
      mIcon11 = BitmapFactory.decodeStream(in); 
     } catch (Exception e) { 
      Log.e("Error", "image download error"); 
      Log.e("Error", e.getMessage()); 
      e.printStackTrace(); 
     } 
     return mIcon11; 
    } 

    protected void onPostExecute(Bitmap result) { 
     //set image of your imageview 
     bmImage.setImageBitmap(result); 
     //close 
     mDialog.dismiss(); 
    } 
} 
+1

終於有人誰做的工作,這樣的例子很多在那裏,其從活動不質疑同一行 – Jonas

+0

tyyy它的作品!最後!!!你爲我節省了時間:) –

+0

終於得到它的工作!,感謝這! – TharakaNirmana

0

試試這個:

AQuery aq = new AQuery(getActivity()); 
aq.id(view.findViewById(R.id.image)).image(imageUrl, true, true, 0, 0, 
    new BitmapAjaxCallback() { 
     @Override 
     public void callback(String url, ImageView iv, Bitmap bm, AjaxStatus status) { 
      iv.setImageBitmap(bm); 
     } 
    }.header("User-Agent", "android")); 
相關問題