如何将imageview从一个活动发送到另一个活动

时间:2012-10-12 11:46:02

标签: android android-intent imageview

我在第一个活动的listview中有一个imageview, 我想将我的imageview发送到listview项目的clicl上的第二个活动。

我尝试过以下代码 -

将可绘制图像转换为bytearray: -

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而不是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();

3 个答案:

答案 0 :(得分:4)

您无需将位图转换为字节数组。位图是可分割的,因此您只需使用putParcelable(String, Parcelable)将其添加到Bundle。

修改

例如:

Bundle extras = new Bundle();
extras.putParcelable("Bitmap", bmp);
intent.putExtras(extras);
startActivity(intent);

然后在第二项活动中:

Bundle extras = getIntent().getExtras();
Bitmap bmp = (Bitmap) extras.getParcelable("Bitmap");

答案 1 :(得分:0)

我想你传递图像ID并在Next Activity Imageview中设置ID E.g。

GEt ID  ((ImageView) v).getId();
SET ID  imageView.setImageResource(imgId);

答案 2 :(得分:0)

您可以将ImageView转换为位图。试试这个

Bitmap bitmap = Bitmap.createBitmap(imageView .getMeasuredWidth(),imageView .getMeasuredHeight(), Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
ImageView .draw(canvas);