我在显示库视图中选择的图像预览时出现问题。我有一张来自SD卡的图片库,在点击图片时,它的预览应显示在图库视图下方(不是通过意图单独活动)。我能够向图库展示图像,但点击图像时没有任何结果。
公共类NewActivity扩展了Activity {
GalleryBaseAdapter myGalleryBaseAdapter;
Gallery myPhotoGallery;
int[] mFiles = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myPhotoGallery = (Gallery)findViewById(R.id.photogallery);
myGalleryBaseAdapter = new GalleryBaseAdapter(this);
String ExternalStorageDirectoryPath = Environment.getExternalStorageDirectory().getAbsolutePath();
String path = ExternalStorageDirectoryPath;
String files;
File folder = new File (path);
final File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++)
{
if (listOfFiles[i].isFile())
{
files = listOfFiles[i].getName();
System .out.println(files);
}
}
for (File file : listOfFiles) {
myGalleryBaseAdapter.add(file.getPath());
}
myPhotoGallery.setAdapter(myGalleryBaseAdapter);
myPhotoGallery.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View v, int position, long id) {
mFiles = new int[listOfFiles.length];
ImageView imageview = (ImageView)findViewById(R.id.imageView1);
Bitmap bitmapImage = BitmapFactory.decodeFile("/sdcard/" + mFiles[position]);
imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageview.setImageBitmap(bitmapImage);
}
});
}
public class GalleryBaseAdapter extends BaseAdapter {
ArrayList<String> GalleryFileList;
Context context;
GalleryBaseAdapter(Context cont){
context = cont;
GalleryFileList = new ArrayList<String>();
}
@Override
public int getCount() {
return GalleryFileList.size();
}
@Override
public Object getItem(int position) {
return GalleryFileList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Bitmap bm = BitmapFactory.decodeFile(GalleryFileList.get(position));
LinearLayout layout = new LinearLayout(context);
layout.setLayoutParams(new Gallery.LayoutParams(150, 150));
layout.setGravity(Gravity.CENTER);
ImageView imageView = new ImageView(context);
imageView.setLayoutParams(new Gallery.LayoutParams(200, 200));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageBitmap(bm);
layout.addView(imageView);
return layout;
}
public void add(String newitem){
GalleryFileList.add(newitem);
}
}
}
任何人都可以告诉我代码中的错误是什么。提前谢谢。
答案 0 :(得分:0)
等等但....你的onItemClick中的mFiles变量是一个int空数组!?!您正在尝试解码路径,该路径是“/ sdcard”和mFiles'row之间的串联,但是......您在哪里填充该数组?我认为你必须指定文件路径,如“listOfFiles [position] .getPath()”