2017-08-24 40 views
0

我試圖從URL加載圖像到位圖,但我得到一個NetworkOnMainThreadException錯誤,但我不知道爲什麼。將URL轉換爲位圖會導致Android中的網絡錯誤

這是我使用的方法:

public Bitmap getBitmapFromURL(String src) { 
    try { 
     java.net.URL url = new java.net.URL(src); 
     HttpURLConnection connection = (HttpURLConnection) url 
       .openConnection(); 
     connection.setDoInput(true); 
     connection.connect(); 
     InputStream input = connection.getInputStream(); 
     Bitmap myBitmap = BitmapFactory.decodeStream(input); 
     return myBitmap; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return null; 
    } 
} 

我也試圖把影像加載使用畢加索庫中的目標,因爲在最後,我想從這個圖片使用調色板的主色。這是我用畢加索的代碼:我把這個我的onCreate方法內

Picasso.with(MovieDetails.this) 
      .load("https://image.tmdb.org/t/p/w500" + backdrop) 
      .into(new Target() { 
       @Override 
       public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { 
        Palette palette = Palette.from(bitmap).generate(); 
        System.out.println(palette.getVibrantSwatch().toString().substring(16, Math.min(palette.getVibrantSwatch().toString().length(), 22))); 
        LinearLayout lLayout = (LinearLayout) findViewById(R.id.layout_bg); 
        lLayout.setBackgroundColor(Color.parseColor("#"+ palette.getVibrantSwatch().toString().substring(16, Math.min(palette.getVibrantSwatch().toString().length(), 22)))); 
       } 

       @Override 
       public void onBitmapFailed(Drawable errorDrawable) { 

       } 

       @Override 
       public void onPrepareLoad(Drawable placeHolderDrawable) { 

       } 
      }); 

,但我正在逐漸「法不覆蓋從其超法」,在所有三個@Override方法中的錯誤。

回答

0

糾正第一個問題的方法是在Thread或Runnable中執行該操作,有一個標誌可以設置像StrictModeEnabled之類的東西,但這很糟糕,不要訴諸於此。

至於畢加索,我不知道從超類的東西壓倒一切的方法,我想嘗試做這件事,而不是onViewCreated的onCreate

+0

感謝您的回答。我應該如何處理線程?我創建了'new thread','run()','try'和'while'(true)'我有:'sleep(1000);' 'handler.post(本);'。我應該如何放入線程?我該怎麼處理依賴於此方法結果的其他方法? –

0

我設法通過,因爲它遵循使用的AsyncTask來解決我的問題:

我把這個在我的活動結束:

public class MyAsync extends AsyncTask<Void, Void, Bitmap>{ 

    @Override 
    protected Bitmap doInBackground(Void... params) { 

     try { 
      URL url = new URL("https://image.tmdb.org/t/p/w500" + backdrop); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      connection.setDoInput(true); 
      connection.connect(); 
      InputStream input = connection.getInputStream(); 
      Bitmap myBitmap = BitmapFactory.decodeStream(input); 
      return myBitmap; 

     } catch (IOException e) { 
      e.printStackTrace(); 
      return null; 
     } 

    } 
} 

請注意,這種方法裏面,我把該URL的圖像。

要訪問的位圖我的活動內,並獲得主導使用調色板我這樣做:

MyAsync obj = new MyAsync(){ 

      @Override 
      protected void onPostExecute(Bitmap bmp) { 
       super.onPostExecute(bmp); 

       Bitmap bm = bmp; 

       if (bm != null && !bm.isRecycled()) { 
        Palette palette = Palette.from(bm).generate(); 
        System.out.println(palette.getVibrantSwatch().toString().substring(16, Math.min(palette.getVibrantSwatch().toString().length(), 22))); 
        LinearLayout lLayout = (LinearLayout) findViewById(R.id.layout_bg); 
        lLayout.setBackgroundColor(Color.parseColor("#"+ palette.getVibrantSwatch().toString().substring(16, Math.min(palette.getVibrantSwatch().toString().length(), 22)))); 
       } 


      } 
     }; 

     obj.execute(); 

此代碼是我的onCreate方法內。

相關問題