如何将活动中的图像发送到另一个活动?

时间:2011-11-24 01:05:17

标签: android

我正在尝试创建一个包含2 Activities的应用。 Activity1是一个图库可以调用Activity2Activity2是一款相机应用,可以捕获图像然后将这些图像发送到Activity1,而Activity1会在图库中显示这些图像。我无法在两个Activities之间发送图像。< / p>

此代码是Activity1中的我的图库。我知道SerializebleBitmap但在我的代码中如何将其放入图库列表?这是我的代码:

public class HelloGallery extends Activity {
private Gallery gallery;
private ImageView imgView;
private Button btn;
private static final int SHOW_SUB_FORM = 0;
private String[] imglist;
Bundle extras = getIntent().getExtras();
private Integer[] mImageIds = {
        R.drawable.sample_0,
        R.drawable.sample_1,
        R.drawable.sample_2,
        R.drawable.sample_3,
        R.drawable.sample_4,
        R.drawable.sample_5,
        R.drawable.sample_6,
        R.drawable.sample_7
};
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  gallery = (Gallery) findViewById(R.id.gallery);
    gallery.setAdapter(new ImageAdapter(this));
   imgView = (ImageView)findViewById(R.id.ImageView01); 
    imgView.setImageResource(mImageIds[0]);

    btn = (Button) findViewById(R.id.Takept);


       btn.setOnClickListener(new View.OnClickListener(){




        public void onClick(View v) {

            btn_onClick();
        }

    }); 



    if (extras != null){

        String[] imglist= extras.getStringArray("IMAGE_LIST");
        imgView.setImageResource(mImageIds[0]);


    }

    gallery.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {
            Toast.makeText(HelloGallery.this, "" + position, Toast.LENGTH_SHORT).show();
            imgView.setImageResource(mImageIds[position]);




        }
    });


};

private void btn_onClick() {

    Intent intent = new Intent(HelloGallery.this,CameraDemo.class);


    startActivity(intent);
} 

public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;


    public ImageAdapter(Context c) {
        mContext = c;
        TypedArray attr = mContext.obtainStyledAttributes(R.styleable.HelloGallery);
        mGalleryItemBackground = attr.getResourceId(
                R.styleable.HelloGallery_android_galleryItemBackground, 0);
        attr.recycle();
    }

    public int getCount() {
        return mImageIds.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);

        imageView.setImageResource(mImageIds[position]);
        imageView.setLayoutParams(new Gallery.LayoutParams(100, 100));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setBackgroundResource(mGalleryItemBackground);

        return imageView;
    }
}
}

3 个答案:

答案 0 :(得分:0)

通常,您不希望将图像从一个活动发送到另一个活动。您将希望活动1将其存储在某个位置,然后告诉活动2去访问它。可能在两个活动都可以访问的另一个基础应用程序类中。

希望这有帮助

答案 1 :(得分:0)

在您的意图中,您可以传递Serializable对象。

请务必,您必须创建一个类来对图像进行serilizing /反序列化:

    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.Serializable;

    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Bitmap.CompressFormat;

    public class SerializableBitmap implements Serializable {
       private static final long serialVersionUID = 1208813848065674061L;
       private Bitmap bitmap;
       // Serialize image
       private void writeObject(java.io.ObjectOutputStream out) throws IOException {
          ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
          bitmap.compress(CompressFormat.JPEG, 100, bos); 
          byte[] bitmapdata = bos.toByteArray();
          out.write(bitmapdata, 0, bitmapdata.length);
       }

       // Deserialize image
       private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
           ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
           int b;
           while((b = in.read()) != -1)
              byteStream.write(b);
           byte bitmapBytes[] = byteStream.toByteArray();
           bitmap = BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.length);
       }

       public Bitmap getBitmap() {
          return bitmap;
       }

       public void setBitmap(Bitmap bitmap) {
          this.bitmap = bitmap;
       }
   }

可以自由地使用setBitmap()和getBitmap()方法来序列化/取消位图,并在你的意图中放置可序列化的位图对象:

    intent.putExtra(extraDataKey, serializableBitmap);

但请注意,如果图像太大,此解决方案可能会导致性能问题。

希望有所帮助。

答案 2 :(得分:0)

您发送图像的位置,如“R.drawable.sample_0”,您将其作为字符串发送,然后当您在活动2中获得它时,您可以从您的drawables中获取它。这都不需要发送图像:)