如何在活动之间传递drawable

时间:2011-12-06 21:48:34

标签: android android-activity drawable

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

我试试这个:

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);

但是它向我报告了一个例子:

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

7 个答案:

答案 0 :(得分:50)

1)将意图传递为附加内容

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

  • 使用此方法(附加)图像以162毫秒的时间间隔传递
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 中,您将收到带有字节数组(解码图片)的意图,并将其作为源应用于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)保存图片文件并将参考传递给其他活动

  

“尺寸限制是:保持尽可能小。绝对不要放   一个位图,除非它不大于一个图标(32x32或   无论)。

  • 在*活动A *中保存文件(内部存储)
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
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("picname", fileName);
  • 在*活动B *中检索文件
Bundle extras = getIntent().getExtras();
String fileName = extras.getString("picname");
  • 使* * drawable *脱离图片
File filePath = getFileStreamPath(fileName);
Drawable d = Drawable.createFromPath(filePath.toString());
  • 将其应用于ImageView资源
someImageView.setBackgroundDrawable(d);

答案 1 :(得分:4)

您可以使用图像资源名称(例如" img1.png")标记每个图像(在xml中或以编程方式), 然后使用getTag();

检索图像名称

然后使用getResources().getIdentifier(image name,"drawable", .getPackageName())获取可绘制资源ID。

只需通过意图传递资源ID -

intent.putExtra("img 1",resource id);

最后,结果Activity可以使用以下命令从资源创建图像:

getResources().getDrawable(intent.getIntExtra("img 1",-1));    

答案 2 :(得分:3)

Drawable个对象本身并不是可序列化的,因此无法直接在Intent个附加内容中传递它们。您必须找到另一种方法来序列化或保留图像数据并在新的Activity中检索它。

例如,如果您正在使用BitmapDrawable个实例,则可以将基础Bitmap写入文件并读回,或序列化为字节数组(如果它足够小)和字节数组可以通过Intent的附加内容传递。

HTH

答案 3 :(得分:2)

最好不要在Drawables中传递(或序列化)Activities。您很可能从资源中获取可绘制的内容。因此,有一个资源ID。相反,传递它,这只是一个int。并重新补充其他 Drawable中的Activity

如果Drawable不是来自资源,而是在运行时在内存中构建......那么让我们谈谈它。 @Devunwired在这种情况下有一个很好的建议。

答案 4 :(得分:0)

如果您从网络中提取这些图像并反复加载它们,那么缓存它们将是非常好的选择,因为您将减少网络流量并提高加载速度。我建议使用WebImageView中的Droid-Fu。非常好的解决方案,在某些项目中使用它,我对它非常满意。

答案 5 :(得分:0)

我不知道是否是这种情况,但是如果您尝试传递drawable的原因是因为您使用的是Imageview,只需将资源ID放在imageview的标记中,将标记作为Integer传递而不是intent的extra中的drawable,并在接收活动中使用以下行:         imageView.setImageDrawable(。getResources()getDrawable(getIntent()getIntExtra( “image_id”,0)));

希望它会对某人有所帮助。

答案 6 :(得分:0)

您只需使用原生@Autowired private Step2ItemProcessor listObject; 方法:

buildDrawingCache

然后在你的ImageActivity上获取它:

    ImageView imageView = imageLayout.findViewById (R.id.img_thumb);
    imageView.buildDrawingCache ();

    Bundle extras = new Bundle ();
    extras.putParcelable ("image", imageView.getDrawingCache ());

    Intent intent = new Intent (this, ImageActivity.class);
    intent.putExtras (extras);

    startActivity (intent);
相关问题