【问题标题】:how to send imageview from one activity to other如何将图像视图从一个活动发送到另一个活动
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 你能详细说明一下吗。捆绑附加服务=新捆绑(); extras.putParcelable("位图", 位图);写了吗?
  • 我们可以通过 Intent 发送多少数据。大尺寸图片会给我带来问题吗?
【解决方案2】:

你传递图像 ID 并在 Next Activity Imageview 中设置 Id 例如

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

【讨论】:

  • 我已将该图像设置为 imageview。你的意思是我应该通过意图将 imageview id 发送到下一个活动。
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    • 2023-03-28
    • 2016-11-04
    • 2012-07-20
    • 1970-01-01
    相关资源
    最近更新 更多