2012-04-03 28 views
1

我有一个应用程序,允许用户从本地图库中选择图片,然后在图像视图窗口小部件中显示此图像。我如何通过android中的活动发送图像

我的问题是: 1我必须发送这个图片到其他活动。我该怎么做。

2,在接收器的工作我应该表现出它在图像视图widget作为图像不链接或东西

我想这个代码,但它给了我一个运行时错误

Bitmap image = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.RGB_565); 
     view.draw(new Canvas(image)); 
     String url = Images.Media.insertImage(getContentResolver(), image,"title", null); 

回答

0

只要按照下面的步骤...

开放的URI = NULL;任何点击事件 1)使用下面的代码打开原生图库

Intent intent = new Intent(); 
intent.setType("image/*"); 
intent.setAction(Intent.ACTION_GET_CONTENT); 
startActivityForResult(Intent.createChooser(intent, "Select Picture"),0); 

这将打开图库中选择图片将返回到您的活动。 OnActivity结果。

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
switch (requestCode) { 
case 0: 
    if (resultCode == RESULT_OK) { 
    try { 
    uri = Uri.parse(data.getDataString()); 
    imageView.setImageUri(uri); 
} catch (FileNotFoundException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} catch (IOException e) { 
// TODO Auto-generated catch block 
    e.printStackTrace(); 
    }} 
    break; 
    } 
} 

2)也强似的图像可以通过URI到下一个活动,你传递字符串在矿井secont活动,您使用的意图得到它。

Intent i = new Intent(this, Second.class); 
i.putExtra("URI", uri.toString()); 
startActivity(i); 

,并在第二个活动现在

String uri = getIntent().getStringExtra("URI"); 

你有串只需将它设置为图像视图像下面

imageView.setImageUri(Uri.parse(uri)); 
+0

我已经做了画廊的一部分,但我需要将它发送到另一个活动,并将其显示为图像。你可以请给出执行这一步骤的代码。 – Maha 2012-04-03 16:55:30

0

使用意图putExtra和发送在Acvtivity1和第二活动选择的图像用户的URI使用意图getExtra读取URI

参考此答案https://stackoverflow.com/a/7325248/308251

+0

好的这是我如何发送正常值,但图像呢?我认为我应该做一些转移到位图,然后将其转换为普通的图像,以显示它在第二个活动,请给我的代码做这一步 – Maha 2012-04-03 16:58:39

+0

但为什么你需要发送位图,你可以发送uri选定的位图它更优化,有没有任何用例,为什么你更喜欢发送位图? – 2012-04-03 17:01:48

0

是的,你可以通过putExtra方法。

Intent i = new Intent(this, Second.class); 
Bitmap b; // your bitmap 
ByteArrayOutputStream bs = new ByteArrayOutputStream(); 
b.compress(Bitmap.CompressFormat.PNG, 50, bs); 
i.putExtra("Arraybyte", bs.toByteArray()); 
startActivity(i); 

并在Second.java

if(getIntent().hasExtra("Arraybyte")) { 
ImageView iv= new ImageView(this); 
Bitmap b = BitmapFactory.decodeByteArray(
    getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);   
iv.setImageBitmap(b); 

}

0

也许这是不是你要找的,这有点穷,但当我需要在活动之间传递对象时挽救了我的生命。

public class MagatzemImg { 
     private static MagatzemImg instance = null; 
     private static Bitmap img; 

     public MagatzemImg(){ 
      img=null; 
     } 

     public static MagatzemImg getInstance() { 
      if (instance == null) 
      instance = new MagatzemImg(); 
      return instance; 
     } 

     public static void setImg(Bitmap im){ 
      img = im; 
     } 
     public static Bitmap getImg(){ 
      Bitmap imgAux = img; 
      img = null; 
      return imgAux; 
     } 
    } 

然后从新的活动:

MagatzemImg.getInstance(); 
    image = MagatzemImg.getImg(); 

可以“保证”,以使图像通过putExtra("image",true)或你喜欢,如检查,如果别的静态类内部存在的新活动“图像”为空。

相关问题