如何通过活动之​​间绘制 [英] How to pass drawable between activities

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

问题描述

如何传递图像,活动之间抽拉型?

我试试这个:

 私人绘​​制对象imagen画质;

束束=新包();
bundle.putSerializable(imagen画质(序列化)unaReceta.getImagen());
意图myIntent =新的意图(v.getContext(),Receta.class);
myIntent.putExtras(包);
startActivityForResult(myIntent,0);
 

不过,报告我一个execption:

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

解决方案

1)的意图传递为演员

活动A 的你去code图像,并通过故意发送:

  • 在使用这种方法(临时演员)的图像传递162毫秒的时间间隔

 位图位= BitmapFactory.de codeResource(getResources(),R.drawable.ic_launcher);
ByteArrayOutputStream BAOS =新ByteArrayOutputStream();
bitmap.com preSS(Bitmap.Com pressFormat.PNG,100,BAOS);
byte []的B = baos.toByteArray();

意向意图=新的意图(这一点,ActivityB.class);
intent.putExtra(图片报,B);
startActivity(意向);
 

b活动的您会收到意向与字节数组(德codeD图片),并将其应用为源,以ImageView的:

 捆绑额外= getIntent()getExtras()。
byte []的B = extras.getByteArray(图片);

BMP位= BitmapFactory.de codeByteArray(B,0,b.length个);
ImageView的形象=(ImageView的)findViewById(R.id.imageView1);

image.setImageBitmap(BMP);
 

2)保存图像文件,并通过它的引用到另一个活动

  • 为什么要这么做 - 摘自<一个href="http://groups.google.com/group/android-developers/browse_frm/thread/9309931b3f060284#">http://groups.google.com/group/android-developers/browse_frm/thread/9309931b3f060284#
  

大小限制是:保持尽可能小,绝对不要把。   在那里,除非是不超过一个图标大位图(32×32或   不管)。

  • 在*活动A *保存文件(内置存储)

 字符串文件名=SomeName.png;
尝试 {
    FileOutputStream中fileOutStream = openFileOutput(文件名,MODE_PRIVATE);
    fileOutStream.write(B); // B是字节数组
                             //(使用,如果你已经下载你的照片
                             //从*网站*,或把它从*设备的摄像头*)
                             //否则该技术是没用的
    fileOutStream.close();
}赶上(IOException异常IOE){
    ioe.printStackTrace();
}
 

  • 在传递位置的String b活动

 意向意图=新的意图(这一点,ActivityB.class);
intent.putExtra(picname,文件名);
 

  • 在*活动B *检索文件

 捆绑额外= getIntent()getExtras()。
字符串文件名= extras.getString(picname);
 

  • 请* *绘制出来的图片

 文件文件路径= getFileStreamPath(文件名);
绘制对象D = Drawable.createFromPath(filePath.toString());
 

  • 在应用它的ImageView的资源

  someImageView.setBackgroundDrawable(D);
 

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

I try this:

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

But it reports me an execption:

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

解决方案

1) Passing in intent as extras

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

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

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) Saving image file and passing its reference to another activity

"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).

  • 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();
}

  • Pass location as String to Activity B

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

  • In *Activity B* retrieve the file

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

  • Make *drawable* out of the picture

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

  • Apply it to the ImageView resource

someImageView.setBackgroundDrawable(d);

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

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