【问题标题】:Convert URL to Bitmap results in a network error in Android将 URL 转换为位图会导致 Android 出现网络错误
【发布时间】:2018-02-02 19:37:35
【问题描述】:

我正在尝试将图像从 URL 加载到位图,但出现 NetworkOnMainThreadException 错误,但我不知道为什么。

这是我正在使用的方法:

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 库将图像加载到 Target,因为最后,我想使用 Palette 从该图像中获取主色。这是我使用毕加索的代码:

    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 方法中,但是在所有三个 @Override 方法中都出现“方法没有覆盖其超类中的方法”错误。

【问题讨论】:

    标签: android picasso android-bitmap loadimage


    【解决方案1】:

    我设法通过使用 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。

    要访问我的 Activity 中的位图并使用 Palette 获取主色,我这样做了:

    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 方法中。

    【讨论】:

      【解决方案2】:

      纠正第一个问题的方法是在 Thread 或 Runnable 中执行该操作,有一个标志您可以设置类似 StrictModeEnabled 或类似的东西,但这很糟糕,不要诉诸于此。

      就毕加索而言,我不知道超类中的重写方法,我会尝试在 onViewCreated 而不是 onCreate 中进行操作

      【讨论】:

      • 感谢您的回答。我应该如何处理线程?我创建了'新线程','run()','try'和'while(true)'我有:'sleep(1000);' 'handler.post(this);'。我应该如何放入线程中?以及依赖于该方法结果的其他方法我该怎么办?
      猜你喜欢
      • 2015-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-14
      • 2011-02-23
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多