2017-08-24 12 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; 
    } 
} 

最後に、私はパレットを使用して、この画像から支配的な色を取得したいので、私はまた、ピカソのライブラリを使用してターゲットにイメージをロードしよう。これは私がピカソを使用しているコードです:

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) { 

       } 
      }); 

私は私のonCreateメソッドの中にこれを入れて、私はすべての3つの@Overrideメソッドでエラー「そのスーパークラスからメソッドをオーバーライドしない方法」を取得しています。

答えて

0

最初の問題を解決する方法は、スレッドまたはランナブルでそのアクションを実行することです。StrictModeEnabledなどの何かを設定できるフラグがありますが、それは悪いです。 :

限りピカソとして、私はスーパーのものからオーバーライドする方法を知っていけない、私の代わりに、それは次のように私はAsyncTaskを使って、私の問題を解決するために、管理のonCreate

+0

お返事ありがとうございます。どのようにスレッドを処理する必要がありますか?私は '新しいスレッド'、 '実行'、 '試して'、 '内部'を作成しました。 'handler.post(this);'どのようにスレッドの中に入れるべきですか?そして、この方法の結果に依存する他の方法で私は何をしますか? –

0

のonViewCreatedでそれをやろうと思います

私は私の活動の終わりにこれを置く:

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を入れていることに注意してください。このコードは、私のonCreateメソッド内で

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(); 

:私の活動の内部でビットマップにアクセスし、私はこれをしたパレットを使用して支配的な色を取得するために

関連する問題