You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio为GridView添加OnItemClick实现SQLite图片跳转全屏查看

实现方案

总共分为3个步骤:

步骤1:新建全屏展示图片的Activity

首先创建布局文件 activity_image_fullscreen.xml,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black">
    <ImageView
        android:id="@+id/iv_full_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitCenter"/>
</LinearLayout>

然后创建对应的Activity类 ImageFullScreenActivity.java:

public class ImageFullScreenActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_fullscreen);
        ImageView ivFull = findViewById(R.id.iv_full_image);
        // 接收传递过来的图片字节数组
        byte[] imageBytes = getIntent().getByteArrayExtra("image_data");
        if (imageBytes != null) {
            Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
            ivFull.setImageBitmap(bitmap);
        }
        // 可选功能:点击图片直接关闭当前全屏页面
        ivFull.setOnClickListener(v -> finish());
    }
}

最后需要在 AndroidManifest.xml 中注册这个Activity,在application标签内添加如下代码:

<activity android:name=".ImageFullScreenActivity" />

步骤2:给AoodList的GridView添加点击事件

打开 AoodList.java,在onCreate方法的 adapter.notifyDataSetChanged(); 之后添加如下代码:

gridView.setOnItemClickListener((parent, view, position, id) -> {
    // 获取当前点击项对应的Aood对象
    Aood clickedAood = list.get(position);
    // 初始化跳转Intent
    Intent intent = new Intent(AoodList.this, ImageFullScreenActivity.class);
    // 传递图片字节数组
    intent.putExtra("image_data", clickedAood.getImage());
    startActivity(intent);
});

可选优化说明

如果你的图片体积较大,超过Intent传递数据的1MB限制,可改用静态变量临时存储图片,修改方式如下:

  • 在AoodList类中添加公共静态变量 public static byte[] tempImage;
  • 点击事件中取消Intent传值,改为赋值 AoodList.tempImage = clickedAood.getImage();
  • 在ImageFullScreenActivity中直接读取 AoodList.tempImage 解析为Bitmap即可,页面销毁时可将静态变量置空避免内存占用。

内容的提问来源于stack exchange,提问作者Franz 77

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 12:57:03