我正在尝试在Gridview中显示特定目录中的本地图像,现在我想在新活动中单击gridview缩略图时显示完整图像。我是android的新手,并且已经在互联网上搜索了过去两天,找不到任何可以帮助我的东西。希望这里的任何人都能提供帮助。 这是我的代码。
PictureRecyclerAdapter.java
private ArrayList<File> filesList;
private Activity activity;
Context context;
public class MyViewHolder extends RecyclerView.ViewHolder {
ImageView thumbnail;
ImageButton imageDownload, imageShare;
public MyViewHolder(View itemView) {
super(itemView);
thumbnail = itemView.findViewById(R.id.pictures_thumbnail);
imageDownload = itemView.findViewById(R.id.imageDownload);
imageShare = itemView.findViewById(R.id.imageShare);
}
}
public pictureRecyclerAdapter(ArrayList<File> filesList, Activity activity) {
this.filesList = filesList;
this.activity = activity;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflatedView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.pictures_item_view, parent, false);
return new MyViewHolder(inflatedView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
final File currentFile = filesList.get(position);
Glide.with(activity).load(currentFile.getAbsolutePath())
.skipMemoryCache( false )
.crossFade().into(holder.thumbnail);
holder.imageDownload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
copyFile(currentFile, new File( DIRECTORY_TO_SAVE_MEDIA_NOW+currentFile.getName()),activity);
} catch (IOException e) {
e.printStackTrace();
}
}
});
holder.thumbnail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent go = new Intent(activity, ImageDetailsActivity.class);
go.putExtra("image", FileProvider.getUriForFile(context,BuildConfig.APPLICATION_ID + ".provider",currentFile));
activity.startActivity(go);
}
});
这是我的PictureActivity
public class pictures extends Fragment {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
private static final String IMAGE_LOCATION = "/storage/emulated/0/Image Folder/";
public pictures() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_pictures, container, false);
recyclerView=(RecyclerView)v.findViewById(R.id.pictures_recyclerView);
layoutManager = new GridLayoutManager(getContext(),2);
recyclerView.setLayoutManager(layoutManager);
recyclerView.addItemDecoration(new ItemDecorationAlbumColumns(
getResources().getDimensionPixelSize(R.dimen.photos_list_spacing),2));
getListFiles(new File(IMAGE_LOCATION));
pictureRecyclerAdapter ra = new pictureRecyclerAdapter(this.getListFiles(new File(IMAGE_LOCATION)), getActivity());
recyclerView.setAdapter(ra);
return v;
}
private ArrayList<File> getListFiles(File parentDir) {
ArrayList<File> inFiles = new ArrayList<File>();
File[] files;
files = parentDir.listFiles();
if (files != null) {
for (File file : files) {
if (file.getName().endsWith(".jpg") ||
file.getName().endsWith(".png") ||
file.getName().endsWith(".jpeg")) {
if (!inFiles.contains(file))
inFiles.add(file);
}
}
}
return inFiles;
}
}
答案 0 :(得分:1)
要在ImageDetailsActivity中显示图像,请按意图传递图像文件路径并开始活动。
void choiceAction(String choice) {
setState((){
switch (choice) {
case ("Sign In"):
//Navigate to the nextScreen value
loggedIn = true;
Navigator.push(
context, MaterialPageRoute(builder: (context) => LoginScreen(0)));
break;
case ("Sign Out"):
loggedIn = false;
setSignedOutState(); //Sets state. So Sign Out to Sign in works just fine.
break;
default:
}
};
}
然后详细活动onCreate方法,获取路径并使用滑行或其他方式显示它。
holder.thumbnail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent go = new Intent(activity, ImageDetailsActivity.class);
go.putExtra("image",currentFile.getAbsolutePath());
activity.startActivity(go);
}
});