gridview 显示sdcard中图片的问题 本帖最后由 Small_ChengXuYuan 于 2014-06-17 15:31:54 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你打印看下picFiles里面的东西呢, 弱弱的问下 我那个SimpleAdapter有问题吗?因为那里我不是很懂 我看有些人这样用 我就拿来用了 好了 终于被我弄出来了 上面的那个也许不是很好理解 这个好点我把代码贴出来 也是用了别人的代码(http://www.eoeandroid.com/thread-174314-1-1.html)额 。代码有点乱 不过这个代码是对的 有些地方我没注意 也不知道重不重要就没管//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++public class MainActivity extends Activity { private int Position= 0; private String fileName = null; private String suffix = null; private File sdcardDir=null; private List<HashMap<String, String>> picFiles = new ArrayList<HashMap<String,String>>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.gallyshow); //this.context = context; boolean isSDExist=Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); if(isSDExist){//如果Sdcard存在执行下面的 sdcardDir=Environment.getExternalStorageDirectory(); String sdPath = sdcardDir.toString(); System.out.println(sdPath); File file = new File(sdPath); File[] files = file.listFiles(); for(int i=0;i<files.length;i++) { if(files[i].isFile()){itIsfile(files[i]);} else{ itNotfile(files[i]);} }} } private void itIsfile(File file) { // TODO Auto-generated method stub fileName = file.getName(); Position=fileName.indexOf("."); GridView gridView = (GridView)findViewById(R.id.gridview); HashMap<String,String> pic = new HashMap<String, String>(); if(Position>0){ suffix=fileName.substring(Position+1); if(suffix.equals("jpg")||suffix.equals("png")){ if(file.length()<20000){ pic.put("image", file.getPath()); picFiles.add(pic); } } } ImageAdapter adapter = new ImageAdapter(MainActivity.this,picFiles); gridView.setAdapter(adapter); } private void itNotfile(File file) { // TODO Auto-generated method stub File[] files = file.listFiles(); if(files==null){return;} for(int i=0;i<files.length;i++) { if(files[i].isFile()){itIsfile(files[i]);} if(!files[i].isFile()){itNotfile(files[i]);} } } //+++++++++++++++++++++++++++++++++++++++++++ImageAdapter适配器+++++++++++++++++++++++++++++++++++ class ImageAdapter extends BaseAdapter{ public ImageAdapter(MainActivity mainActivity, List<HashMap<String, String>> picFiles) { // TODO Auto-generated constructor stub } @Override public int getCount() { // TODO Auto-generated method stub System.out.println("+++++++++++++++++++++++++++文件大小+++++++++++++++++++++++++++++++++++"); System.out.println(picFiles.size()); return picFiles.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return picFiles.get(arg0); } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public View getView(int arg0, View arg1, ViewGroup arg2) { // TODO Auto-generated method stub //picFiles.get(arg0); //System.out.println("+++++++++++++++++++++++++文件位置+++++++++++++++++++++++++++++"); //System.out.println(picFiles.get(arg0)); //HashMap<String, String> hash = picFiles.get(arg0); ImageView imageview = new ImageView(MainActivity.this); Map<String,String> fsPath = picFiles.get(arg0); String image=(String)fsPath.get("image"); File fl=new File(image); try { FileInputStream fin=new FileInputStream(fl); BufferedInputStream bin=new BufferedInputStream(fin); Bitmap bm=BitmapFactory.decodeStream(bin); Bitmap thbm=ThumbnailUtils.extractThumbnail(bm, 150,150); //System.out.println("++++++++++++++++++显示到gridview中的图片++++++++++++++++++++"); //System.out.println(thbm); imageview.setImageBitmap(thbm);//将略缩图设置到ImageView上 //bin.close(); fin.close(); bm.recycle(); } catch(Exception e){ } return imageview; } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; }} 一个号只能回复自己三次 没办法 上小号 //++++++++++++++++++++++++++++++++++唉 其实第一次犯的错和第二次都一样 都是把private HashMap<String,String> pic = new HashMap<String, String>();这个放错位置的缘故 把他的位置改为第二个那样就对了,其它地方都没问题 是对的 只不过代码太乱了 啊 要死啊 感觉什么都不懂啊 估计我说了半天 也没说明白啥 好了 换号 结贴嘞! 请问一下楼主 我的什么也显示不出来 是一个空白 是不是要先在sdcard添加图片资源? 关于viewpager+gridview的问题 activity传值跳转以后出现问题。。。 Android浏览器插件示例编译问题 apk反编译jar问题 如何能打印出trackball的状态。 Android中如何调用zlib xml中的引用 android canvas读取背景图片,涂鸦之后保存 请问anroid怎么播放aac文件。 Android Studio代码不执行变化 Android 遍历多叉树 打印根节点到子节点 这个版本的ADT,zuo si 的节奏吗?!
private int Position= 0;
private String fileName = null;
private String suffix = null;
private File sdcardDir=null;
private List<HashMap<String, String>> picFiles = new ArrayList<HashMap<String,String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallyshow);
//this.context = context;
boolean isSDExist=Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
if(isSDExist){//如果Sdcard存在执行下面的
sdcardDir=Environment.getExternalStorageDirectory();
String sdPath = sdcardDir.toString();
System.out.println(sdPath);
File file = new File(sdPath);
File[] files = file.listFiles();
for(int i=0;i<files.length;i++)
{ if(files[i].isFile()){itIsfile(files[i]);}
else{ itNotfile(files[i]);}
}}
}
private void itIsfile(File file) {
// TODO Auto-generated method stub
fileName = file.getName();
Position=fileName.indexOf(".");
GridView gridView = (GridView)findViewById(R.id.gridview);
HashMap<String,String> pic = new HashMap<String, String>();
if(Position>0){
suffix=fileName.substring(Position+1);
if(suffix.equals("jpg")||suffix.equals("png")){
if(file.length()<20000){
pic.put("image", file.getPath());
picFiles.add(pic); }
}
}
ImageAdapter adapter = new ImageAdapter(MainActivity.this,picFiles);
gridView.setAdapter(adapter);
}
private void itNotfile(File file) {
// TODO Auto-generated method stub
File[] files = file.listFiles();
if(files==null){return;}
for(int i=0;i<files.length;i++)
{ if(files[i].isFile()){itIsfile(files[i]);}
if(!files[i].isFile()){itNotfile(files[i]);}
}
}
//+++++++++++++++++++++++++++++++++++++++++++ImageAdapter适配器+++++++++++++++++++++++++++++++++++
class ImageAdapter extends BaseAdapter{ public ImageAdapter(MainActivity mainActivity,
List<HashMap<String, String>> picFiles) {
// TODO Auto-generated constructor stub
} @Override
public int getCount() {
// TODO Auto-generated method stub
System.out.println("+++++++++++++++++++++++++++文件大小+++++++++++++++++++++++++++++++++++");
System.out.println(picFiles.size());
return picFiles.size();
} @Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return picFiles.get(arg0);
} @Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
} @Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
//picFiles.get(arg0);
//System.out.println("+++++++++++++++++++++++++文件位置+++++++++++++++++++++++++++++");
//System.out.println(picFiles.get(arg0));
//HashMap<String, String> hash = picFiles.get(arg0);
ImageView imageview = new ImageView(MainActivity.this);
Map<String,String> fsPath = picFiles.get(arg0);
String image=(String)fsPath.get("image");
File fl=new File(image);
try {
FileInputStream fin=new FileInputStream(fl);
BufferedInputStream bin=new BufferedInputStream(fin);
Bitmap bm=BitmapFactory.decodeStream(bin);
Bitmap thbm=ThumbnailUtils.extractThumbnail(bm, 150,150);
//System.out.println("++++++++++++++++++显示到gridview中的图片++++++++++++++++++++");
//System.out.println(thbm);
imageview.setImageBitmap(thbm);//将略缩图设置到ImageView上
//bin.close();
fin.close();
bm.recycle();
}
catch(Exception e){
}
return imageview;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}}
//++++++++++++++++++++++++++++++++++
唉 其实第一次犯的错和第二次都一样 都是把private HashMap<String,String> pic = new HashMap<String, String>();这个放错位置的缘故 把他的位置改为第二个那样就对了,其它地方都没问题 是对的 只不过代码太乱了
啊 要死啊 感觉什么都不懂啊 估计我说了半天 也没说明白啥 好了 换号 结贴嘞!