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メソッドでエラー「そのスーパークラスからメソッドをオーバーライドしない方法」を取得しています。
お返事ありがとうございます。どのようにスレッドを処理する必要がありますか?私は '新しいスレッド'、 '実行'、 '試して'、 '内部'を作成しました。 'handler.post(this);'どのようにスレッドの中に入れるべきですか?そして、この方法の結果に依存する他の方法で私は何をしますか? –