如何将图像发送到android中的另一个活动?
我有一组我在imageview的time.What的指定的时间间隔,我想,当我点击图片我要显示在另一个activity.How该图像期间显示阵列的我能做到这一点如何将图像发送到android中的另一个活动?
代码: -
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);
}
不传递位图对象。
而不是传递可绘制资源的id。
Intent intent = new Intent(MainActivity.this, Second.class);
intent.putExtra("image_res_ids", CollectonUtils.join("/", imageArray);
而且在Second
活动,
getIntent().getExtra("image_res_ids");
获取图像资源ID阵列。
你的图像是可绘制的资源,它们只不过是一个int。所以你可以把它们作为额外的意图传递给Intent。
................................ .... – Raghav
希望当我点击图片我要显示在另一个活动的形象。
使用img
setTag()
方法保存当前绘制ID和拿回来就来看点击使用getTag()
设置绘制ID使用setTag:
....
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
以在另一个活动中显示点击的图像。
以及如何获得额外 – Raghav
@Raghav:使用'INT draw_id = getIntent()getIntExtra( “clicked_img_draw_id”,-1)。在第二个活动的创造中 –
您使用setTag保存索引/资源id,即如下执行img.setTag(index)。 而fowarding它,你可以从标签删除索引/资源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());
}
});
只是尝试发送您所选择的图像的位置
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/
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.
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);
}
}
尝试了这一点,它肯定会工作..希望这会帮助你。
尝试使用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
和自定义为自动旋转,而不是目前使用的一种。如果Activity
已被破坏
你也可以传递数组列表的位置,使ArrayList的静态的,所以你可以在全球使用它
Runnable
可能导致内存泄漏。 –如何................. – Raghav
定义res/values/array.xml文件并仅将位置传递给下一个活动。在下一个活动中访问数组并获取图像:) –