Android 将图像从four square加载到gallery适配器
您好,我正在创建一个应用程序,它从Foursquare中提取资源并将其显示在android手机上,我在加载图像时遇到了问题。该应用程序运行,但由于某些原因,它没有加载到手机上,它只显示我定义的加载程序图像 我的代码如下:Android 将图像从four square加载到gallery适配器,android,Android,您好,我正在创建一个应用程序,它从Foursquare中提取资源并将其显示在android手机上,我在加载图像时遇到了问题。该应用程序运行,但由于某些原因,它没有加载到手机上,它只显示我定义的加载程序图像 我的代码如下: public class PicAdapter extends BaseAdapter { //use the default gallery background image int defaultItemBackground; //gallery
public class PicAdapter extends BaseAdapter {
//use the default gallery background image
int defaultItemBackground;
//gallery context
private Context galleryContext;
//array to store bitmaps to display
private Bitmap[] imageBitmaps;
//placeholder bitmap for empty spaces in gallery
Bitmap placeholder;
//constructor
public PicAdapter(Context c) {
//instantiate context
galleryContext = c;
//create bitmap array
//imageBitmaps = new Bitmap[10];
//decode the placeholder image
//placeholder = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
}
//BaseAdapter methods
//return number of data items i.e. bitmap images
public int getCount() {
return PicturesURLlist.size();
}
//return item at specified position
public Object getItem(int position) {
return position;
}
//return item ID at specified position
public long getItemId(int position) {
return position;
}
//get view specifies layout and display options for each thumbnail in the gallery
public View getView(int position, View convertView, ViewGroup parent) {
//create the view
ImageView imageView = new ImageView(galleryContext);
//specify the bitmap at this position in the array
//imageView.setImageBitmap(imageBitmaps[position]);
ImageLoader imgloader = new ImageLoader(getApplicationContext());
int loader = R.drawable.ic_launcher;
HashMap<String, String> song = new HashMap<String, String>();
song = PicturesURLlist.get(position);
//Log.d("Links", song.get(picurl));
imgloader.DisplayImage(song.get(picurl), loader, imageView );
//imageView.setImageBitmap((imgloader.DisplayImage(song.get(picurl), loader, imageView)));
//set layout options
imageView.setLayoutParams(new Gallery.LayoutParams(400, 600));
//scale type within view area
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
//set default gallery item background
imageView.setBackgroundResource(defaultItemBackground);
//return the view
return imageView;
}
//custom methods for this app
//helper method to add a bitmap to the gallery when the user chooses one
}
公共类PicAdapter扩展了BaseAdapter{
//使用默认的库背景图像
国际背景;
//画廊背景
私人背景画廊背景;
//存储要显示的位图的数组
私有位图[]图像位图;
//库中空白的占位符位图
位图占位符;
//建造师
公共PicAdapter(上下文c){
//实例化上下文
galleryContext=c;
//创建位图数组
//imageBitmaps=新位图[10];
//解码占位符图像
//占位符=BitmapFactory.decodeResource(getResources(),R.drawable.ic_启动器);
}
//BaseAdapter方法
//返回数据项的数量,即位图图像
public int getCount(){
返回PicturesURLlist.size();
}
//在指定位置返回项目
公共对象getItem(int位置){
返回位置;
}
//返回指定位置的项目ID
公共长getItemId(int位置){
返回位置;
}
//获取视图为库中的每个缩略图指定布局和显示选项
公共视图getView(int位置、视图转换视图、视图组父视图){
//创建视图
ImageView ImageView=新的ImageView(galleryContext);
//指定阵列中此位置的位图
//setImageBitmap(图像位图[位置]);
ImageLoader imgloader=新的ImageLoader(getApplicationContext());
int loader=R.drawable.ic_启动器;
HashMap宋=新HashMap();
song=PicturesURLlist.get(位置);
//Log.d(“链接”,song.get(picurl));
imgloader.DisplayImage(song.get(picurl)、loader、imageView);
//setImageBitmap((imgloader.DisplayImage(song.get(picurl),loader,imageView));
//设置布局选项
setLayoutParams(新的Gallery.LayoutParams(400600));
//视图区域内的比例类型
imageView.setScaleType(imageView.ScaleType.FIT_CENTER);
//设置默认库项目背景
setBackgroundResource(defaultItemBackground);
//返回视图
返回图像视图;
}
//此应用程序的自定义方法
//帮助器方法,在用户选择位图时将其添加到库中
}
我的imagloader课程
public class ImageLoader {
MemoryCache memoryCache=new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews=Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService;
public ImageLoader(Context context){
fileCache=new FileCache(context);
executorService=Executors.newFixedThreadPool(5);
}
int stub_id = R.drawable.ic_launcher;
public String DisplayImage(String url, int loader, ImageView imageView)
{
stub_id = loader;
imageViews.put(imageView, url);
Bitmap bitmap=memoryCache.get(url);
Log.d("Image loader succes", url);
if(bitmap!=null){
imageView.setImageBitmap(bitmap);
Log.d("Image loader succes", "true");
}
else
{
queuePhoto(url, imageView);
imageView.setImageResource(loader);
Log.d("Image loader succes", "false");
}
return null;
}
private void queuePhoto(String url, ImageView imageView)
{
PhotoToLoad p=new PhotoToLoad(url, imageView);
executorService.submit(new PhotosLoader(p));
}
private Bitmap getBitmap(String url)
{
File f=fileCache.getFile(url);
//from SD cache
Bitmap b = decodeFile(f);
if(b!=null)
return b;
//from web
try {
Bitmap bitmap=null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is=conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Exception ex){
ex.printStackTrace();
return null;
}
}
//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
try {
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE=70;
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
//Task for the queue
private class PhotoToLoad
{
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i){
url=u;
imageView=i;
}
}
class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad;
PhotosLoader(PhotoToLoad photoToLoad){
this.photoToLoad=photoToLoad;
}
public void run() {
if(imageViewReused(photoToLoad))
return;
Bitmap bmp=getBitmap(photoToLoad.url);
memoryCache.put(photoToLoad.url, bmp);
if(imageViewReused(photoToLoad))
return;
BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad);
Activity a=(Activity)photoToLoad.imageView.getContext();
a.runOnUiThread(bd);
}
}
boolean imageViewReused(PhotoToLoad photoToLoad){
String tag=imageViews.get(photoToLoad.imageView);
if(tag==null || !tag.equals(photoToLoad.url))
return true;
return false;
}
//Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable
{
Bitmap bitmap;
PhotoToLoad photoToLoad;
public BitmapDisplayer(Bitmap b, PhotoToLoad p){bitmap=b;photoToLoad=p;}
public void run()
{
if(imageViewReused(photoToLoad))
return;
if(bitmap!=null)
photoToLoad.imageView.setImageBitmap(bitmap);
else
photoToLoad.imageView.setImageResource(stub_id);
}
}
public void clearCache() {
memoryCache.clear();
fileCache.clear();
}
公共类ImageLoader{
MemoryCache MemoryCache=新的MemoryCache();
文件缓存文件缓存;
private Map ImageView=Collections.synchronizedMap(新的WeakHashMap());
执行服务执行服务;
公共图像加载器(上下文){
fileCache=新的fileCache(上下文);
executorService=Executors.newFixedThreadPool(5);
}
int stub_id=R.drawable.ic_启动器;
公共字符串DisplayImage(字符串url、int加载器、ImageView ImageView)
{
存根id=装载机;
put(imageView,url);
位图位图=memoryCache.get(url);
Log.d(“图像加载成功”,url);
if(位图!=null){
设置图像位图(位图);
Log.d(“图像加载程序成功”、“正确”);
}
其他的
{
队列照片(url、imageView);
setImageResource(加载程序);
Log.d(“图像加载程序成功”、“错误”);
}
返回null;
}
私有void队列照片(字符串url,ImageView)
{
PhotoToLoad p=新的PhotoToLoad(url,imageView);
executorService.submit(新的PhotoLoader(p));
}
私有位图getBitmap(字符串url)
{
文件f=fileCache.getFile(url);
//从SD缓存
位图b=解码文件(f);
如果(b!=null)
返回b;
//从网络
试一试{
位图=空;
URL imageUrl=新URL(URL);
HttpURLConnection conn=(HttpURLConnection)imageUrl.openConnection();
连接设置连接超时(30000);
连接设置读取超时(30000);
conn.setInstanceFollowRedirects(真);
InputStream is=conn.getInputStream();
OutputStream os=新文件OutputStream(f);
Utils.CopyStream(is,os);
os.close();
位图=解码文件(f);
返回位图;
}捕获(例外情况除外){
例如printStackTrace();
返回null;
}
}
//对图像进行解码和缩放以减少内存消耗
私有位图解码文件(文件f){
试一试{
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码流(新的FileInputStream(f),null,o);
//找到正确的刻度值。它应该是2的幂。
所需的最终int_尺寸=70;
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=1;
while(true){
如果(width_tmp/2)是您在scrollview中的适配器?您可以尝试将固定链接加载到simple imageview以测试您的imageloaderr是否工作。根据您的信息,很难确定它是否不加载。@AndroidLearner它是一个多媒体资料适配器。@Yul我尝试过,效果很好。imageview.setBackgroundResource这行是什么意思(defaultItemBackground);
?它不加载或强制关闭?