我当前正在使用nhaarman gridview,我想从自定义文件夹中获取位图。 gridview是片段,它将从“test”文件夹中提取图像并显示它。 模拟器能够运行此代码,但不能在真实手机中运行。 有人请帮忙。感谢。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_home_fragment_month, container, false);
GridView gridView = (GridView) view.findViewById(R.id.activity_gridview_gv);
//---------------------------------------------------------------------------
file = new File(Environment.getExternalStorageDirectory()
+ File.separator + "Test");
// Check for SD Card
if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
Toast.makeText(getActivity(), "Error! No SDCARD Found!", Toast.LENGTH_LONG)
.show();
} else {
if(file.exists()==false){
// Locate the image folder in your SD Card
file = new File(Environment.getExternalStorageDirectory()
+ File.separator + "Test");
// Create a new folder if no folder named Test exist
file.mkdirs();
}
}
if (file.isDirectory()) {
listFile = file.listFiles();
// Create a String array for FilePathStrings
FilePathStrings = new String[listFile.length];
// Create a String array for FileNameStrings
FileNameStrings = new String[listFile.length];
for (int i = 0; i < listFile.length; i++) {
// Get the path of the image file
FilePathStrings[i] = listFile[i].getAbsolutePath();
// Get the name image file
FileNameStrings[i] = listFile[i].getName();
}
}
SwingBottomInAnimationAdapter swingBottomInAnimationAdapter = new SwingBottomInAnimationAdapter(new MyAdapter(getActivity().getApplicationContext(), getItems()));
swingBottomInAnimationAdapter.setAbsListView(gridView);
swingBottomInAnimationAdapter.setInitialDelayMillis(300);
adapter = new GridViewAdapter(getActivity(), FilePathStrings, FileNameStrings);
//gridView.setAdapter(swingBottomInAnimationAdapter);
gridView.setAdapter(adapter);
return view;
}
公共类GridViewAdapter扩展了BaseAdapter {
// Declare variables
private Context activity;
private String[] filepath;
// file path , Environment.getExternalStorageDirectory()+ File.separator + "test"
private String[] filename;
private LayoutInflater inflater = null;
public GridViewAdapter(Context a, String[] fpath, String[] fname) {
activity = a;
filepath = fpath;
filename = fname;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return filepath.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = (ImageView) convertView;
if (imageView == null) {
imageView = new ImageView(activity);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
// Decode the filepath with BitmapFactory followed by the position
Bitmap bmp = BitmapFactory.decodeFile(filepath[position]);
// Set the decoded bitmap into ImageView
imageView.setImageBitmap(bmp);
return imageView;
}
}
答案 0 :(得分:0)
您是在设备上部署apk还是使用它进行测试?因为如果要在真实设备上部署apk,添加权限可能是一个问题。