图像传递到另一个活动 [英] Passing image to another activity

查看:107
本文介绍了图像传递到另一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想出如何activites感谢之间的字符串值传递到这个网站,但我有过一个形象的麻烦。什么我想的就是有一个用户点击打开的库,并允许选择图片的按钮。然后,我有一个会打开并显示一个ImageView的另一个活动的另一个按钮。我希望能有一个ImageView的形象是被选中的人从previous活动。

I figured out how to pass a String value between activites thanks to this site, however I'm having trouble passing an image. What I'm trying to to is have a user click a button that opens the gallery and allows selecting of a picture. Then I have another button that opens another activity that displays an ImageView. I want to be able to have that ImageView's image be the chosen one from the previous activity.

下面是有按钮,我点击打开库和检索所选择的图像类:

Here is the class that has the button I'm clicking to open the gallery and retrieve the chosen image:

public class EnterEdit extends Activity implements View.OnClickListener 
{
private static final int SELECT_IMAGE = 0;

 String filepath;

 Bundle fieldresults;
 Intent b; 

 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.enteredit);

  Button selectwallpaper = (Button) findViewById(R.id.selectwallpaper);
  selectwallpaper.setOnClickListener(this);

  Button previewwallpaper = (Button) findViewById(R.id.previewwallpaper);
  previewwallpaper.setOnClickListener(this);

  fieldresults = new Bundle();
  b = new Intent(this, PreviewScreen.class);
 }


 @Override
 public void onClick(View view) 
 {
  switch (view.getId())
  {
   case R.id.selectwallpaper:
    Intent gallery = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(gallery, SELECT_IMAGE);
    break;

   case R.id.previewwallpaper:
     startActivity(b); 
    }
    break;   
  }


 protected void onActivityResult(int requestCode, int resultCode, Intent data)
 {
  super.onActivityResult(requestCode, resultCode, data);

   if (resultCode == RESULT_OK)
   {
    if (requestCode == SELECT_IMAGE)
    {
     Uri selectedimage = data.getData();
     String[] filepathcolumn = {MediaStore.Images.Media.DATA};

     Cursor cursor = getContentResolver().query(selectedimage, filepathcolumn, null, null, null);
     cursor.moveToFirst();

     int columnindex = cursor.getColumnIndex(filepathcolumn[0]);
     filepath = cursor.getString(columnindex);
     cursor.close();

     fieldresults.putString("bitmap", filepath);
     b.putExtras(fieldresults);
    }
   }
 }
}

这里是要显示所选择的图像类:

And here is the class that should display the chosen image:

public class PreviewScreen extends Activity implements View.OnClickListener 
{

 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.previewscreen);

  Bundle fieldresults = this.getIntent().getExtras();
  String backgroundpath = fieldresults.getString("bitmap");

  String background = BitmapFactory.decodeFile(backgroundpath);
  ImageView gallerypic = (ImageView) findViewById(R.id.gallerypic);
  gallerypic.setImageBitmap(background); 
 }
}

什么我不知道的是,在 OnActivityResult 如果我要通过 selectedImage 或chosenimage在 b.putExtra(位图,selectedimage); 行。我都试过,但我没有看到图片上的第二个活动。此外,我不知道在 previewScreen 类,如果我正确设置ImageView的。任何帮助是AP preciated。谢谢你。

What I'm not sure about is in the OnActivityResult if I should pass the selectedImage or the chosenimage in the b.putExtra("bitmap", selectedimage); line. I tried both but I didn't see an image on the second activity. Also I wasn't sure in the PreviewScreen class if I'm setting the imageview correctly. Any help is appreciated. Thanks.

推荐答案

相信我,你不想活动之间进行传递的图像。相反,你为什么不只是传递图片的路径,让第二个活动决定如何处理这条道路(德code和显示图像,在这种情况下)。

Trust me, you don't want to be passing images between activities. Rather, why don't you simply pass the path of the image and let the second activity decide what to do with that path (decode and display the image, in this case).

这篇关于图像传递到另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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