如何将图像从一个活动发送到另一个活动?
我正在尝试制作一个具有 2 个 Activity 的应用程序。 Activity1
是一个画廊,可以调用Activity2
。 Activity2
是一个相机应用程序,它捕获图像然后将这些图像发送到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一般来说,您不希望将图像从一个活动发送到另一个活动。您将希望活动 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
根据您的意图,您可以传递可序列化的对象。
为此,您必须创建一个用于序列化/反序列化图像的类,如下所示:
free 以使用 setBitmap() 和 getBitmap() 方法来序列化/反序列化您的位图,并在您的意图中放入可序列化位图对象:
填充 如果图像太大,解决方案可能会导致性能问题。
希望有帮助。
In your intent, your have possibility to pass Serializable objects.
Do to, you must create a class for serilizing/deserializing your image like this:
Fill free to use setBitmap() and getBitmap() methods to serialize/deserilize your bitmap, and in your intent put the serializable bitmap object:
But consider that this solution could cause performance problems if the image is too large.
Hope it help.
您发送图像的位置,如“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 :)