在实现了gridview显示文件内图片的功能之后,想要添加点击图片跳转页面显示图片的效果。
但是只能完成跳转不能完成显示
这是文件图片预览界面
点击后跳转页面
album.java
package com.example.mycamera2021a;
import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class album extends AppCompatActivity {
/**
* 显示图片的GridView
*/
private GridView gvPhoto;
/**
* 文件夹下所有图片的bitmap
*/
private List<Bitmap> listpath;
/**
* 文件夹下图片的真实路径
*/
private String scanpath;
/**
* 显示图片的适配器
*/
private Photodaapter adapter;
/**
* 所有图片的名字
*/
public String[] allFiles;
/**
* 想要查找的文件夹
*/
private File folder;
/**
* 返回主界面
*/
private Button btn1;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.album);
btn1 = (Button) findViewById(R.id.return1);
btn1.setOnClickListener(new View.OnClickListener() {
/**
* 跳转功能
* @param
*/
@Override
public void onClick(View v) {
Intent i = new Intent(album.this, MainActivity.class);
startActivity(i);
}
});
/**将gvphoho部署在GridView中*/
gvPhoto = (GridView) findViewById(R.id.view1);
initData();
/**给GridView中添加点击控件*/
gvPhoto.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(album.this, photo.class);
intent.putExtra("path",allFiles[position]);
startActivity(intent);
}
});
}
private void initData() {
listpath = new ArrayList<>();
folder = new File(Environment.getExternalStorageDirectory().toString(),"/mycamera");
/**将文件夹下所有文件名存入数组*/
allFiles = folder.list();
/**遍历数组*/
for (int i = 0; i < allFiles.length; i++) {
scanpath = folder + "/" + allFiles[i];
/**将文件转为bitmap如果为空则不是图片文件*/
Bitmap bitmap = BitmapFactory.decodeFile(scanpath);
if (bitmap != null) {
listpath.add(bitmap);
}
}
/** 图片写入适配器*/
adapter = new Photodaapter(listpath, this);
gvPhoto.setAdapter(adapter);
}
/**
* item适配器
*/
public class Photodaapter extends BaseAdapter {
private List<Bitmap> mlist;
private Context mcontext;
private LayoutInflater minflater;
private int selected = -1;
public Photodaapter(List<Bitmap> list, Context context) {
super();
this.mlist = list;
this.mcontext = context;
this.minflater = LayoutInflater.from(context);
}
@Override
public int getCount() { return mlist.size(); }
@Override
public Object getItem(int position) {
return mlist.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
VIewHolder vh;
if (convertView == null) {
vh = new VIewHolder();
convertView = minflater.inflate(R.layout.item, null);
vh.iv = (ImageView) convertView.findViewById(R.id.iv_grid);
convertView.setTag(vh);
} else {
vh = (VIewHolder) convertView.getTag();
}
vh.iv.setImageBitmap(mlist.get(position));
Bitmap path = mlist.get(position);
return convertView;
}
}
class VIewHolder {
ImageView iv;
}
}
photo.java
package com.example.mycamera2021a;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.io.ByteArrayOutputStream;
public class photo extends AppCompatActivity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photo);
imageView = (ImageView)findViewById(R.id.imageView_cen);
Intent it = getIntent();
final int imageRId = it.getExtras().getInt("path");
imageView.setImageResource(imageRId);
}
}