我的SDCard上有一个用.nomedia隐藏的文件夹。我有这个Imageadapter,它在GridView中显示文件夹(图像和视频)的内容。它们似乎都出现了,但有时当我点击我知道的视频缩略图时,会显示图像甚至是不同的视频。有人可以告诉我这里哪里出错吗?
GridViewImageAdapter.class
package com.soboapps.todos.adapter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class GridViewImageAdapter extends BaseAdapter {
private Activity mActivity;
private ArrayList<String> mFilePaths = new ArrayList<String>();
private int imageWidth;
public GridViewImageAdapter(Activity activity, ArrayList<String> filePaths, int imageWidth) {
this.mActivity = activity;
this.mFilePaths = filePaths;
this.imageWidth = imageWidth;
}
@Override
public int getCount() {
return this.mFilePaths.size();
}
@Override
public Object getItem(int position) {
return this.mFilePaths.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mActivity);
if(mFilePaths.get(position).contains("jpg") || mFilePaths.get(position).contains("jpeg") ||
mFilePaths.get(position).contains("png"))
{
Bitmap image = BitmapFactory.decodeFile(mFilePaths.get(position)); //Creation of Thumbnail of image
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(imageWidth, imageWidth));
imageView.setImageBitmap(image);
}
else if(mFilePaths.get(position).contains(".mp4") || mFilePaths.get(position).contains(".3gp"))
{
Bitmap video = ThumbnailUtils.createVideoThumbnail(mFilePaths.get(position), imageWidth); //Creation of Thumbnail of video
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(imageWidth, imageWidth));
imageView.setImageBitmap(video);
}
}
else
{
imageView = (ImageView)convertView;
}
return imageView;
}
/*
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mActivity);
} else {
imageView = (ImageView) convertView;
}
// get screen dimensions
Bitmap image = decodeFile(mFilePaths.get(position), imageWidth, imageWidth);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(imageWidth, imageWidth));
imageView.setImageBitmap(image);
return imageView;
}
*/
public static Bitmap decodeFile(String filePath, int WIDTH, int HIGHT) {
try {
File f = new File(filePath);
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
final int REQUIRED_WIDTH = WIDTH;
final int REQUIRED_HIGHT = HIGHT;
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_WIDTH
&& o.outHeight / scale / 2 >= REQUIRED_HIGHT)
scale *= 2;
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
}
答案 0 :(得分:1)
在getView()函数内部,如果convertView不为null,则不能正确设置缩略图。像这样重新排列代码来处理这种情况:
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mActivity);
} else {
imageView = (ImageView) convertView;
}
if(mFilePaths.get(position).contains("jpg") || mFilePaths.get(position).contains("jpeg") ||
mFilePaths.get(position).contains("png"))
{
Bitmap image = BitmapFactory.decodeFile(mFilePaths.get(position)); //Creation of Thumbnail of image
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(imageWidth, imageWidth));
imageView.setImageBitmap(image);
}
else if(mFilePaths.get(position).contains(".mp4") || mFilePaths.get(position).contains(".3gp"))
{
Bitmap video = ThumbnailUtils.createVideoThumbnail(mFilePaths.get(position), imageWidth); //Creation of Thumbnail of video
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(imageWidth, imageWidth));
imageView.setImageBitmap(video);
}
return imageView;
}
convertView参数不是已经初始化到指定位置的视图,您可以按原样返回。它是一个用于显示不同网格元素的视图,它被回收用于不同的位置,因此您需要进行设置。
此外,如果您遇到滞后的UI问题,请考虑在AsyncTask或线程池中创建缩略图,而不是在主UI线程上创建缩略图。查看此类问题,了解有关如何执行此操作的信息:Loading images in a gridview with async task