我正在尝试使用以下代码在名为 GalleryFrag 的片段中创建带有网格视图的图库
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import java.io.File;
import java.util.ArrayList;
/**
* A simple {@link Fragment} subclass.
*/
public class GalleryFrag extends Fragment {
ArrayList<String> f = new ArrayList<>();
File[] files;
public GalleryFrag() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_gallery, container, false);
GridView gridView = (GridView) view.findViewById(R.id.gv);
gridView.setAdapter(new ImageAdapter(this.getContext()));
getImages();
return view;
}
public void getImages() {
File file = new File(android.os.Environment.getExternalStoragePublicDirectory(DIRECTORY_PICTURES));
if (file.isDirectory()) {
files = file.listFiles();
for (int i = 0; i < files.length; i++) {
f.add(files[i].getAbsolutePath());
}
}
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context context) {
mContext = context;
}
@Override
public int getCount() {
return f.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
//if it's not recycled, initialize some atributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new ViewGroup.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(Integer.parseInt(f.get(position)));
return imageView;
}
}
}
但是当我运行该应用程序时,它在包含图像的名为"图片"的文件夹中的 Gridview 中不显示任何内容。我想在用户单击网格视图中的任何图像时全屏显示图像。也可以使用任何不同的方法。请帮助我为我的应用程序创建图库。答案真的是受欢迎的。
代码更新我用这段代码来检查文件存在:
public void getImages() {
File file = new File(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)));
if (file.exists()){
Toast.makeText(this.getContext(), "Yep It's there", Toast.LENGTH_LONG).show();
files = file.listFiles();
if (files !=null) {
for (int i = 0; i < files.length; i++) {
//String[] FileNames= new String[files.length];
//FileNames[i]=files[i].getName();
//ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this.getContext(), android.R.layout.simple_list_item_1, FileNames);
//listView.setAdapter(arrayAdapter);
f.add(files[i].getAbsolutePath());
}
} else {
Toast.makeText(this.getContext(), "error", Toast.LENGTH_SHORT).show();
}
}
}
更新我也需要在图库中添加子文件夹,并且需要从网格升级到回收器视图。我需要在全屏单击时显示图像,但在其他活动中显示图像,而不是在带有弹出图像查看器的同一片段中显示图像。
您可以使用此 liabrary 导入一个简单的库,而无需太多代码:
图片库
https://github.com/lawloretienne/ImageGallery/blob/master/README.md
示例用法
Intent intent = new Intent(MainActivity.this, ImageGalleryActivity.class);
String[] images = getResources().getStringArray(R.array.unsplash_images);
Bundle bundle = new Bundle();
bundle.putStringArrayList(ImageGalleryActivity.KEY_IMAGES, new ArrayList<>(Arrays.asList(images)));
bundle.putString(ImageGalleryActivity.KEY_TITLE, "Unsplash Images");
intent.putExtras(bundle);
startActivity(intent);
中格馆
https://github.com/mzelzoghbi/ZGallery
示例用法
ZGallery.with(this, /*your string arraylist of image urls*/)
.setToolbarTitleColor(ZColor.WHITE) // toolbar title color
.setGalleryBackgroundColor(ZColor.WHITE) // activity background color
.setToolbarColorResId(R.color.colorPrimary) // toolbar color
.setTitle("Zak Gallery") // toolbar title
.show();
你在数组列表中保存值之前给适配器充气,所以你的数组列表是空白的。应用此代码:-
public class GalleryFrag extends Fragment {
ArrayList<String> f = new ArrayList<>();
File[] files;
public GalleryFrag() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_gallery, container, false);
GridView gridView = (GridView) view.findViewById(R.id.gv);
getImages();
gridView.setAdapter(new ImageAdapter(this.getContext()),f);
return view;
}
public void getImages() {
File file = new File(android.os.Environment.getDataDirectory(), "injan");
if (file.isDirectory()) {
files = file.listFiles();
for (int i = 0; i < files.length; i++) {
f.add(files[i].getAbsolutePath());
}
}
}
您的适配器
public class ImageAdapter extends BaseAdapter {
private Context mContext;
ArrayList<String> f;
public ImageAdapter(Context context, ArrayList<String> fData ) {
mContext = context;
f=fData ;
}
@Override
public int getCount() {
return f.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
//if it's not recycled, initialize some atributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new ViewGroup.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(Integer.parseInt(f.get(position)));
return imageView;
}
}
}
一开始就不存在它在 Android/Data/injan 中的文件夹中的 Gridview 中不显示任何内容
具有该名称/路径的文件夹。
Filefile = new File(android.os.Environment.getDataDirectory((, "injan"(;
该目录"/data/injan"只能在有根设备上访问。那么,您是如何将图像放入其中的呢?您的数组列表不包含元素,因此不会显示任何内容。
imageView.setImageResource(Integer.parseInt(f.get(position(((;
预期的文件名是否只有编号?它们是否等于资源标识符?很奇怪这是如何工作的。
不过,在列出之前设置适配器可以按照您实现它的方式进行。
为了使您的应用能够读取/写入文件系统,您应该在清单文件中请求所需的权限。
对于 Android 6+,您需要添加代码,让用户在运行时确认这些请求。
谷歌的运行时权限。
您今年是#1254,遇到了这个问题。