最初のアクティビティのリストビューにイメージビューがあります。イメージビューをリストビュー項目のクリックで 2 番目のアクティビティに送信したいと考えています。
次のコードを試しました-
描画可能な画像をバイト配列に変換します:-
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
インテント経由で送信-
Intent intent=new Intent(PicturesList.this,PictureDetail.class);
intent.putExtra("Bitmap", byteArray);
startActivity(intent);
2回目の活動では
Bundle extras = getIntent().getExtras();
byteArray = extras.getByteArray("Bitmap");
と
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
imageview.setImageBitmap(bmp);
しかし、問題はここにあります-
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
これにはドローアブル イメージが必要で、イメージ ビューがあります。イメージ ビューをドローアブルに変換できますか? それとも何か?drawable の代わりに imageview を送信する方法。誰もがこれを行ったことがあります。
これは、イメージビューで画像を設定する方法です
new AsyncTask<Void,Void,Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
URL newurl = new URL("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png");
bitmap= BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
//bitmap = Bitmap.createScaledBitmap(bitmap, 50,50, true);
}
catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// bitmap=imageLoader.DisplayImage("http://farm3.static.flickr.com/2199/2218403922_062bc3bcf2.jpg", imageview);
//bitmap = Bitmap.createScaledBitmap(bitmap, imageview.getWidth(), imageview.getHeight(), true);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
imageview.setImageBitmap(bitmap);
}
}.execute();