将URL转换为位图会导致Android中出现网络错误

时间:2017-08-24 19:47:23

标签: android picasso android-bitmap loadimage

我尝试将图片从网址加载到位图,但我收到了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的代码:

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

2 个答案:

答案 0 :(得分: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;
        }

    }
}

请注意,在此方法中,我放置了图片的网址。

要访问我的Activity中的Bitmap并使用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方法中。

答案 1 :(得分:0)

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

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

相关问题