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

5

我是一名有用的助手,可以为您进行文本翻译。

我在第一个活动中的列表视图中有一个ImageView,当单击列表项时,我希望将我的ImageView发送到第二个活动中。

我尝试了以下代码 -

将可绘制图像转换为字节数组:

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转换成可绘制吗?或者还有其他方法吗?如何发送ImageView而不是可绘制对象?有人以前做过这个吗?

这是我在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();

你怎样将图片设置到ImageView中呢?其实这要完全取决于具体情况。 - Andro Selva
我已经使用URL在ImageView上设置了图片。当点击它时,我想在第二个活动中显示它。 - Ani
没问题。但我的问题是你用什么形式存储来自URL的图像。你应该使用drawable吧? - Andro Selva
为什么不直接发送图像资源ID(这里似乎是ic_launcher)? - njzk2
如何发送它?(您是指通过意图吗) - Ani
显示剩余2条评论
3个回答

4
您不需要将Bitmap转换为字节数组。Bitmap是可包装的,因此您可以使用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");

你能详细说明一下吗?Bundle extras = new Bundle(); extras.putParcelable("Bitmap", bitmap); 这样写可以吗? - Ani
我们能通过Intent发送多少数据?大尺寸的图片会对我造成问题吗? - Ani

0

我认为你需要传递图片ID并在下一个活动中设置ID到ImageView中,例如:

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

我已经将那张图片设置到ImageView上了。你的意思是我应该通过Intent将ImageView的id传递给下一个活动吗? - Ani

0
你可以将你的ImageView转换为Bitmap。试试这个:
Bitmap bitmap = Bitmap.createBitmap(imageView .getMeasuredWidth(),imageView .getMeasuredHeight(), Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
ImageView .draw(canvas);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接