Java 对于循环打印三次,或者可能是其他的
我试图在下面的链接中更改这个答案 将特定文件夹中的图像显示到gridview布局。我肯定是我弄坏了…查看代码和日志Java 对于循环打印三次,或者可能是其他的,java,android,for-loop,android-imageview,android-gridview,Java,Android,For Loop,Android Imageview,Android Gridview,我试图在下面的链接中更改这个答案 将特定文件夹中的图像显示到gridview布局。我肯定是我弄坏了…查看代码和日志 package nsixty.crew.app; import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import android.content.Context; import android.graphics.Bitmap; im
package nsixty.crew.app;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
public class ImageAdapter45 extends BaseAdapter {
private Context mContext;
public ImageAdapter45(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some
// attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
// File path = new File(Environment.getExternalStorageDirectory(),"/Crews/Images/");
// String paths = null;
//
// if(path.exists())
// {
//
//
//
//
// String[] fileNames = path.list();
//Log.d("I be mumu", data);
///after you use your images
// System.gc();
//Bitmap mBitmap = null;
//for(int i = 0; i < fileNames.length; i++)
//{
// Boolean finish = true;
//
//paths = (path.getPath() + "/" + fileNames[i]);
// BitmapFactory.Options opts=new BitmapFactory.Options();
// opts.inDither=false; //Disable Dithering mode
// opts.inPurgeable=true;
// opts.inPurgeable=true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared
// opts.inInputShareable=true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
// opts.inTempStorage=new byte[32 * 1024];
//System.gc();
File path = new File(Environment.getExternalStorageDirectory(),
"/Crews/Images/");
String paths = null;
// String[] fileNames = null ;
StringBuilder sb = new StringBuilder();
if (path.exists()) {
//ArrayList<String> urls = new ArrayList<String>();
String[] fileNames = path.list();
//List<String> urls =(Arrays.asList(fileNames));
// for (int i = 0; i < fileNames.length; i++) {
//// sb.append((path.getPath() + "/" + fileNames[i]) + "\n");
// paths = ((path.getPath() + "/" + fileNames[i]));
// urls.add(paths);
//
// //Log.v("YELLLO12",urls.toArray().toString());
//
//
//
// }
// urls.add(paths);
// String paths3 = "";
//
// for (String s : urls)
// {
// paths3 += s + "\n";
//
// // imageView.setImageBitmap(BitmapFactory.decodeFile((path.getPath() + "/" + paths3)));
// }
imageView.setImageBitmap(BitmapFactory.decodeFile((path.getPath() + "/" +fileNames[position])));
System.gc();
Log.v("YELLLO1237P",fileNames[position]);
//imageView.setImageBitmap(BitmapFactory.decodeFile("/mnt/sdcard/Crews/Images/IMG_06_11_2012_01_16_05.jpg"));
//Log.v("YELLLO300",path.getPath() + "/" +paths3);
//Bitmap mBitmap = BitmapFactory.decodeFile(paths3);
//imageView.setImageBitmap(mBitmap);
//Log.v("YELLLO123",paths);
//Log.v("YELLLO124ggh",fileNames.toString());
}
//path.close();
// }
//fos.close();
return imageView;
}
//static String data = ImageAdapter45.data3();
//static String[] mThumbIds = {data};
// public static String Pathss(){
// File path = new File(Environment.getExternalStorageDirectory(),"/Crews/Images/");
// String paths = null;
// String[] fileNames = null ;
//
// StringBuilder sb = new StringBuilder();
// if(path.exists())
// {
// fileNames = path.list();
//
// for(int i = 0; i < fileNames.length; i++)
// {
// sb.append((path.getPath()+"/"+ fileNames[i]));
// paths = sb.toString();
// }
//
// }
// return paths.trim();
// }
public Integer mThumbIds() {
File path = new File(Environment.getExternalStorageDirectory(),"/Crews/Images/");
//String paths = null;
String[] fileNames = null ;
if(path.exists())
{
fileNames = path.list();
}
return fileNames.length;
//Log.d("I be mumu", data);
// StringBuilder sb = new StringBuilder();
// for(int i = 0; i < fileNames.length; i++)
// {
// //Boolean finish = true;
// //if (finish == true){
// String paths = (path.getPath() + "/" + fileNames[i]);
// sb.append
// //Log.d("o men see fuckup", paths);
//
// // }
// //fos.close();
//
//
// }
//
}
// references to our images
}
提前感谢。可能是因为您正在使用
File path = new File(Environment.getExternalStorageDirectory(),
"/Crews/Images/");
在getview内部。。。Getview将运行listview中的项目数量,每当您滚动它时,Getview将再次加载。它就像一个循环。尝试在它之外做图像工作。试试看。(将评论转换为答案)
我想主要的问题(除了OutOfMemory
错误)是不正确的getView
,它对位置没有依赖性。将路径3
替换为文件名[位置]
修复了此问题
请注意,您还可以在ImageAdapter45构造函数中检索一次
文件名
,并在mThumbIds()
和getView()
中使用它。我看不到getView内部对position参数的依赖性,所以我想总是得到相同的图像是合乎逻辑的。您好,谢谢回复。请给出一个在getView中添加依赖项的示例,使用(在getView中)文件名[position]代替paths3是否足够?它在我的模拟器上工作,但在手机上显示内存不足错误位图大小超出Vm预算。。我将添加System.gc。。看看它是否能正常工作还是一样。。。模拟器只有两张图片,这可能就是它工作的原因。。但是fone有10张照片。。我正在更新代码,这样你就可以看到它现在的样子了。嗨,谢谢你的回复。你知道我如何规避它吗。请给我举个例子。。。我要申请。。
File path = new File(Environment.getExternalStorageDirectory(),
"/Crews/Images/");