我有一组数组,我在指定的时间间隔内在imageview中显示。当我点击图像时,我想要在另一个活动中显示该图像。我怎么能这样做
代码: -
int[] imageArray = {R.drawable.amazon, R.drawable.app_me,
R.drawable.borabora, R.drawable.dubai};
public void showImage(){
m_oHandler = new Handler();
Runnable oRunnable = new Runnable() {
int i = 0;
@Override
public void run() {
img.setImageResource(imageArray[i]);
i++;
if (i > imageArray.length - 1) {
i = 0;
}
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Didn't know where to go
Intent intent = new Intent(MainActivity.this, Second.class);
intent.putExtra("BitmapImage", imageArray);
}
});
m_oHandler.postDelayed(this, 6000);
}
};
m_oHandler.postDelayed(oRunnable, 6000);
}
答案 0 :(得分:1)
不传递Bitmap对象 而是传递可绘制资源的id。
Intent intent = new Intent(MainActivity.this, Second.class);
intent.putExtra("image_res_ids", CollectonUtils.join("/", imageArray);
在Second
活动中,
getIntent().getExtra("image_res_ids");
获取图像资源ID数组。
答案 1 :(得分:1)
当我点击图片时,我希望在另一个活动中显示该图像。
使用img
setTag()
方法保存当前的可绘制ID,并使用getTag()
1。使用setTag设置drawable id:
....
img.setImageResource(imageArray[i]);
img.setTag(imageArray[i]);
....
2。在onClick方法中从v
获取ID:
int click_drawable_id=Integer.parseInt(v.getTag().toString());
intent.putExtra("clicked_img_draw_id", click_drawable_id);
现在,不要传递imageArray
数组,只需使用clicked_img_draw_id
获取可绘制的ID并将其传递给setImageResource
,以便在另一个活动中显示点击的图片。
答案 2 :(得分:0)
你的图像是可绘制的资源,它们只不过是一个int。所以你绝对可以在Intent中将它们作为额外内容传递。
答案 3 :(得分:0)
您可以将该数组作为额外发送,然后使用getIntent().getIntArrayExtra(EXTRA_IMAGE)
有关详细信息,请查看Intents
答案 4 :(得分:0)
使用setTag保存索引/资源ID,即img.setTag(index),如下所示。 然后,您可以从标记中删除索引/资源ID,并将其作为额外的意图传递
@Override
public void run() {
img.setImageResource(imageArray[i]);
img.setTag(i)
i++;
//your code
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Didn't know where to go
Intent intent = new Intent(MainActivity.this, Second.class);
intent.putExtra("IMAGE_RESOURCE", img.getTag());
}
});
答案 5 :(得分:0)
尝试发送所选图片的位置
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
获得意图
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
imageView.setImageResource(imageAdapter.mThumbIds[position]);
选中此项以获取更多http://www.androidhive.info/2012/02/android-gridview-layout-tutorial/
答案 6 :(得分:0)
If you want to pass single image to next activity, than in your case, only pass image index like
Intent intent = new Intent(MainActivity.this, Second.class);
intent.putExtra("BitmapImage", imageArray[i]);
and on next activity get it. like
int image_id= getIntent().getIntExtra("BitmapImage",0);
this image_id is your selected image resouce id and you can set this as setImageresouce to image view.
答案 7 :(得分:0)
previewView = (ImageView) findViewById(R.id.imgPostIssue);
第一项活动
Intent intent = new Intent(AddNewIssue.this, PostNewIssue.class);
intent.putExtra("picture", byteArray);
finish();
startActivity(intent);
第二项活动
Bundle extras = getIntent().getExtras();
if (extras != null) {
byte[] byteArray = extras.getByteArray("picture");
if (byteArray != null) {
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imgPostIssue);
image.setImageBitmap(bmp);
}
}
试试这个,它肯定会有效。希望它能帮到你。
答案 8 :(得分:0)
尝试使用setTag()
。看看这个
BitmapCache.java
public class BitmapCache {
private static SparseArray<Bitmap> _bitmapCache = new SparseArray<>();
public static void fillBitmapCache(@NonNull Resources resources) {
if (null == _bitmapCache)
_bitmapCache = new SparseArray<>();
if (_bitmapCache.indexOfKey(R.drawable.amazon) < 0)
_bitmapCache.append(R.drawable.amazon, BitmapFactory.decodeResource(resources, R.drawable.amazon));
if (_bitmapCache.indexOfKey(R.drawable.app_me) < 0)
_bitmapCache.put(R.drawable.app_me, BitmapFactory.decodeResource(resources, R.drawable.app_me));
if (_bitmapCache.indexOfKey(R.drawable.borabora) < 0)
_bitmapCache.put(R.drawable.borabora, BitmapFactory.decodeResource(resources, R.drawable.borabora));
if (_bitmapCache.indexOfKey(R.drawable.dubai) < 0)
_bitmapCache.put(R.drawable.dubai, BitmapFactory.decodeResource(resources, R.drawable.dubai));
}
public static Bitmap getAt(int position) {
return get(keyAt(position));
}
public static int keyAt(int position) {
return _bitmapCache.keyAt(position);
}
public static Bitmap get(@DrawableRes int resId) {
return _bitmapCache.get(resId);
}
public static boolean has(@DrawableRes int resId) {
return _bitmapCache.indexOfKey(resId) < 0;
}
public static void append(@DrawableRes int resId, @NonNull Resources resources) {
_bitmapCache.append(resId, BitmapFactory.decodeResource(resources, resId));
}
public static void put(@DrawableRes int resId, @NonNull Resources resources) {
_bitmapCache.put(resId, BitmapFactory.decodeResource(resources, resId));
}
public static int size() {
return _bitmapCache.size();
}
}
活动优先
private void showImage() {
BitmapCache.fillBitmapCache(getResources());
m_oHandler = new Handler();
Runnable oRunnable = new Runnable() {
int i = 0;
@Override
public void run() {
img.setImageBitmap(BitmapCache.getAt(i));
img.setTag(BitmapCache.keyAt(i));
i++;
if (i >= BitmapCache.size()) {
i = 0;
}
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (null != v.getTag()) {
int resId = (int) v.getTag();
Intent intent = new Intent(MainActivity.this, Second.class);
intent.putExtra("BitmapImage", resId);
}
}
});
m_oHandler.postDelayed(this, 6000);
}
};
m_oHandler.postDelayed(oRunnable, 6000);
}
第二项活动
private void displayImageOnSecond() {
Bundle bundle = getIntent().getExtras();
int resId = bundle.getInt("BitmapImage", 0);
if (resId > 0) {
secondImage.setImageBitmap(BitmapCache.get(resId));
}
}
另外,我建议使用ViewPager
并将其自定义为自动旋转而不是您当前使用的旋转。如果Runnable
已被销毁,Activity
可能会导致内存泄漏