【发布时间】:2012-10-12 11:46:01
【问题描述】:
我在第一个活动的列表视图中有一个图像视图, 我想将我的 imageview 发送到 listview 项的 clicl 上的第二个活动中。
我已经尝试了以下代码-
将可绘制图像转换为字节数组:-
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 intent=new Intent(PicturesList.this,PictureDetail.class);
intent.putExtra("Bitmap", byteArray);
startActivity(intent);
在第二个活动中-
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);
这需要可绘制的图像,我有 imageview, 我可以将我的图像视图转换为可绘制的吗?还是什么? 如何发送 imageview 而不是 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();
【问题讨论】:
-
首先如何将 Image 设置为 ImageView?这实际上很大程度上取决于此。
-
我已经使用 URL 在 imageview 上设置了图像。点击它我想在第二个活动中显示它。
-
是的,没关系。但是我的问题是您以什么形式存储来自 url 的图像。你应该用过drawable吧?
-
为什么不直接发送图片resId(这里好像是ic_launcher)?
-
请把你下载的代码贴出来,把Image设置成ImageView。
标签: android android-intent imageview