如何在活动之间传递drawable [英] How to pass drawable between activities

查看:18
本文介绍了如何在活动之间传递drawable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在活动之间传递图像、可绘制类型?

How can I pass an image, drawable type between activities?

我试试这个:

private Drawable imagen;

Bundle bundle = new Bundle();
bundle.putSerializable("imagen", (Serializable) unaReceta.getImagen());
Intent myIntent = new Intent(v.getContext(), Receta.class);
myIntent.putExtras(bundle);
startActivityForResult(myIntent, 0);

但它向我报告了一个 execption:

But it reports me an execption:

java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable

推荐答案

1) 将 Intent 作为 extras

1) Passing in intent as extras

Activity A 中,您解码图像并通过意图发送:

In the Activity A you decode your image and send it via intent:

  • 使用此方法(额外)图像以 162 毫秒的时间间隔传递
  • Using this method (extras) image is passed in 162 milliseconds time interval
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);     
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); 
byte[] b = baos.toByteArray();

Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("picture", b);
startActivity(intent);

Activity B 中,您接收带有字节数组(解码图片)的 Intent 并将其作为源应用到 ImageView:

In Activity B you receive intent with byte array (decoded picture) and apply it as source to ImageView:

Bundle extras = getIntent().getExtras();
byte[] b = extras.getByteArray("picture");

Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);

image.setImageBitmap(bmp);

2) 保存图像文件并将其引用传递给另一个活动

2) Saving image file and passing its reference to another activity

  • WHY to do so? - taken from http://groups.google.com/group/android-developers/browse_frm/thread/9309931b3f060284#

"大小限制是:尽量小.绝对不要放除非它不大于图标(32x32 或随便).

"The size limit is: keep it as small as possible. Definitely don't put a bitmap in there unless it is no larger than an icon (32x32 or whatever).

  • 在 *Activity A* 中保存文件(内部存储)

    • In *Activity A* save the file (Internal Storage)

      String fileName = "SomeName.png";
      try {
          FileOutputStream fileOutStream = openFileOutput(fileName, MODE_PRIVATE);
          fileOutStream.write(b);  //b is byte array 
                                   //(used if you have your picture downloaded
                                   // from the *Web* or got it from the *devices camera*)
                                   //otherwise this technique is useless
          fileOutStream.close();
      } catch (IOException ioe) {
          ioe.printStackTrace();
      }
      

      • 将位置作为字符串传递给活动 B

        • Pass location as String to Activity B

          Intent intent = new Intent(this, ActivityB.class);
          intent.putExtra("picname", fileName);
          

          • 在*Activity B*中检索文件

            • In *Activity B* retrieve the file

              Bundle extras = getIntent().getExtras();
              String fileName = extras.getString("picname");
              

              • 从图片中制作*drawable*

                • Make *drawable* out of the picture

                  File filePath = getFileStreamPath(fileName);
                  Drawable d = Drawable.createFromPath(filePath.toString());
                  

                  • 将其应用于 ImageView 资源

                    • Apply it to the ImageView resource

                      someImageView.setBackgroundDrawable(d);
                      

                      这篇关于如何在活动之间传递drawable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆