一位潜在的雇主要求我制作一个简单的应用程序,该应用程序将从JSON数据加载到recyclervIEw.
问题是:我不能使用任何第三方库来加载图像.
有谁知道我可以使用的当前教程.我在网上搜索过,但似乎有不同的方法,但是很遗憾,到目前为止,我一直非常依赖Picasso和GlIDe.
我的代码基本上是设置为从AsyncTask加载程序获取JsON数据,并在加载程序的onLoadFinish()中设置recyclervIEw和适配器.
理想情况下,我想通过调用AsyncTask从适配器的onBindVIEwHolder中获取url图像.那可行吗?还是我应该另辟do径.
谢谢你的帮助.
解决方法:
我找到了解决方案,您可以查看此链接以获取更多详细描述
Android Hive – Load images from URL-HTTP
清单权限:
<!-- Internet Permissions --> <uses-permission androID:name="androID.permission.INTERNET" /> <!-- Permission to write to external storage --> <uses-permission androID:name="androID.permission.WRITE_EXTERNAL_STORAGE" />
MainActivity代码:
// Loader image - will be shown before loading image int loader = R.drawable.loader; // ImagevIEw to show ImageVIEw image = (ImageVIEw) findVIEwByID(R.ID.image); // Image url String image_url = "https://API.androIDhive.info/images/sample.jpg"; // ImageLoader class instance ImageLoader imgLoader = new ImageLoader(getApplicationContext()); // whenever you want to load an image from url // call displayImage function // url - image url to load // loader - loader image, will be displayed before getting image // image - ImageVIEw imgLoader.displayImage(image_url, loader, image);
ImageLoader类:
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 voID displayImage(String url, int loader, ImageVIEw imageVIEw) { stub_ID = loader; imageVIEws.put(imageVIEw, url); Bitmap bitmap=memoryCache.get(url); if(bitmap!=null) imageVIEw.setimageBitmap(bitmap); else { queuePhoto(url, imageVIEw); imageVIEw.setimageResource(loader); } } 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; } @OverrIDe 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(); }}
文件缓存类:
public class fileCache { private file cacheDir; public fileCache(Context context){ //Find the dir to save cached images if (androID.os.Environment.getExternalStorageState().equals(androID.os.Environment.MEDIA_MOUNTED)) cacheDir=new file(androID.os.Environment.getExternalStorageDirectory(),"TempImages"); else cacheDir=context.getCacheDir(); if(!cacheDir.exists()) cacheDir.mkdirs(); } public file getfile(String url){ String filename=String.valueOf(url.hashCode()); file f = new file(cacheDir, filename); return f; } public voID clear(){ file[] files=cacheDir.Listfiles(); if(files==null) return; for(file f:files) f.delete(); }}
记忆类别:
public class MemoryCache { private Map<String, SoftReference<Bitmap>> cache=Collections.synchronizedMap(new HashMap<String, SoftReference<Bitmap>>()); public Bitmap get(String ID){ if(!cache.containsKey(ID)) return null; SoftReference<Bitmap> ref=cache.get(ID); return ref.get(); } public voID put(String ID, Bitmap bitmap){ cache.put(ID, new SoftReference<Bitmap>(bitmap)); } public voID clear() { cache.clear(); }}
实用程序类:
public class Utils { public static voID copyStream(inputStream is, OutputStream os) { final int buffer_size=1024; try { byte[] bytes=new byte[buffer_size]; for(;;) { int count=is.read(bytes, 0, buffer_size); if(count==-1) break; os.write(bytes, 0, count); } } catch(Exception ex){} }}
总结 以上是内存溢出为你收集整理的android-将图像从url加载到recyclerview,而无需第三方库全部内容,希望文章能够帮你解决android-将图像从url加载到recyclerview,而无需第三方库所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)