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

发布于 2024-12-17 20:56:59 字数 3124 浏览 0 评论 0原文

我正在尝试制作一个具有 2 个 Activity 的应用程序。 Activity1是一个画廊,可以调用Activity2Activity2 是一个相机应用程序,它捕获图像然后将这些图像发送到 Activity1,而 Activity1 将在图库中显示这些图像。我无法发送图像两个Activity之间。

此代码是我在 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;
    }
}
}

I am trying to make an App that have 2 Activities. Activity1 is a gallery can call Activity2. Activity2 is a camera app that capture images then send these images to Activity1 and Activity1 will show these images in gallery.I can't send images between two Activities.

This code is my gallery in Activity1. I know SerializebleBitmap but in my code how can I put it in the gallery list? Here is my code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜血缘 2024-12-24 20:56:59

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

希望这有帮助

In general, you dont want to be sending an image from one activity to another. You will want activity 1 to store it somewhere, and then tell activity 2 to go and access it. Possibly in another base application class that both activities can access.

Hope this helps

左耳近心 2024-12-24 20:56:59

根据您的意图,您可以传递可序列化的对象。

为此,您必须创建一个用于序列化/反序列化图像的类,如下所示:

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

free 以使用 setBitmap() 和 getBitmap() 方法来序列化/反序列化您的位图,并在您的意图中放入可序列化位图对象:

    intent.putExtra(extraDataKey, serializableBitmap);

填充 如果图像太大,解决方案可能会导致性能问题。

希望有帮助。

In your intent, your have possibility to pass Serializable objects.

Do to, you must create a class for serilizing/deserializing your image like this:

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

Fill free to use setBitmap() and getBitmap() methods to serialize/deserilize your bitmap, and in your intent put the serializable bitmap object:

    intent.putExtra(extraDataKey, serializableBitmap);

But consider that this solution could cause performance problems if the image is too large.

Hope it help.

戏蝶舞 2024-12-24 20:56:59

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

you send the location of the image like "R.drawable.sample_0" you send it as string and then when you get it in activity 2, you reach it from your drawables . that's all no need to send the image :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文