2017-08-24 89 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方法内。

相关问题