【发布时间】:2013-12-04 13:46:42
【问题描述】:
我正在尝试让 GridView 显示指定文件中的所有图像,它正在这样做。
但是图像没有出现在网格中,而是不稳定的......见截图
我不知道是什么原因造成的,无论是代码还是与图像大小有关。
任何关于代码解决方案或其他方面的帮助都会很棒
下面是 GridView 和 Adapter 的代码
public class MyWallpapers extends Activity {
private ImageAdapter imageAdapter;
ImageView Selected;
String ImageLocation = null;
ArrayList<String> f = new ArrayList<String>();// list of file paths
File[] listFile;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wallpaper_view_activity);
getFromSdcard();
GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
imageAdapter = new ImageAdapter();
imagegrid.setAdapter(imageAdapter);
imagegrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, final int position, long id) {
//Toast.makeText(MyWallpapers.this, "" + listFile[position].getAbsolutePath(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MyWallpapers.this, WallpaperCrop.class);
intent.putExtra("FilePath",listFile[position].getAbsolutePath());
startActivity(intent);
}
});
}
public void getFromSdcard()
{
File file= new File(android.os.Environment.getExternalStorageDirectory(),"Wallpaper");
if (file.isDirectory())
{
listFile = file.listFiles();
for (int i = 0; i < listFile.length; i++)
{
f.add(listFile[i].getAbsolutePath());
}
}
}
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ImageAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return f.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(
R.layout.galleryitem, null);
holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
Bitmap myBitmap = BitmapFactory.decodeFile(f.get(position));
holder.imageview.setImageBitmap(myBitmap);
return convertView;
}
}
class ViewHolder {
ImageView imageview;
}
}
画廊项目.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/thumbImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
和 wallpaper_view_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/seachpage" >
<GridView
android:id="@+id/PhoneImageGrid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:src="@drawable/bottomcurve" />
</RelativeLayout>
【问题讨论】:
-
你不应该在主线程上解码文件,这是一个严格的模式违规。
-
向我们展示您的 R.layout.galleryitem.xml 文件。我认为您在某处使用 match_parent ,这导致了问题
-
添加了 xml 文件...我知道我在发布时遗漏了一些东西
标签: java android image gridview