在 Android 中全屏显示图像

Display image in Full Screen in Android

我一直在为 Android 编写一个画廊应用程序,因为我刚刚开始使用它。之前我设法让它与可绘制文件夹中的照片一起工作,但现在我已经实现了从存储中获取图像,我似乎无法弄清楚如何获取 ID 以传递给 ShowPic 方法。

这是我的 Main Activity:

的代码
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

public class MainActivity extends AppCompatActivity
{
    private static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1;
    private Cursor cursor;
    private int columnIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
        {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
        }

        String[] projection = {MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID};
        cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.Thumbnails._ID + "");
        columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);

        GridView imgGridView = findViewById(R.id.imgGridView);
        PicAdapter picAdapter = new PicAdapter(MainActivity.this, cursor, columnIndex);
        imgGridView.setAdapter(picAdapter);
        imgGridView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
            
            }
        });
    }


    public void ShowPic(final int imgID)
    {
        Intent intent = new Intent(MainActivity.this, OpenPicFull.class);
        intent.putExtra("picId", imgID);
        startActivity(intent);
    }
}

这是我的适配器:

public class PicAdapter extends BaseAdapter
{
    Context mCont;
    Cursor mCur;
    ImageView mImageView;
    int mColumnIndex;

    public PicAdapter(Context cont, Cursor cur, int index)
    {
        mCont = cont;
        mCur = cur;
        mColumnIndex = index;
    }

    @Override
    public int getCount()
    {
        return mCur.getCount();
    }

    @Override
    public Object getItem(int position)
    {
        return position;
    }

    @Override
    public long getItemId(int position)
    {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        mImageView = new ImageView(mCont);
        mCur.moveToPosition(position);
        int imageID = mCur.getInt(mColumnIndex);
        mImageView.setImageURI(Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + imageID));

        mImageView.setLayoutParams(new GridView.LayoutParams(330, 330));
        mImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        mImageView.setPadding(4, 4, 4, 4);
        return mImageView;
    }
}

这是我的 OpenFullPic class:

public class OpenPicFull extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_open_pic_full);

        ImageView fullImgView = findViewById(R.id.pic_full);
        int picID = getIntent().getExtras().getInt("picId");
        fullImgView.setImageResource(picID);
    }
}

任何帮助将不胜感激 谢谢

所以做你在适配器中做的同样的事情:

imgGridView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
cursor.moveToPosition(position);
ShowPic(cursor.getInt(columnIndex));
            }
        });
    }

然后用这个改变你的 Openfullpic:

fullImgView.setImageURI(Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + picID));

还有一个建议尝试优化你的图片,因为如果有很多图片,你的代码会显示内存错误