Project operating results:
MainActivity the former two figures can be displayed, followed by two figures can not be displayed! ! !
Engage in a long time do not understand
, do not know with the picture Resolution There is no relationship The following items posted major code, Guiqiu big solutions showed ~~~
1, the cache major code
public class AsyncBitmapLoader
{
/ **
* Memory picture soft reference buffer
* /
private HashMap & lt; String, SoftReference & lt; Bitmap & gt; & gt; imageCache = null;
/ **
* Picture Cache time
* /
private static final int DELETE_TIME_INTERVAL = 1000 * 60 / * * 60 * 24 * 10 * /;
private static File cacheDir;
public AsyncBitmapLoader ()
{
imageCache = new HashMap & lt; String, SoftReference & lt; Bitmap & gt; & gt; ();
}
public void clear ()
{
cacheDir = null;
if (imageCache! = null)
{
try
{
releaseBitmapCache ();
}
catch (Exception e)
{
}
imageCache.clear ();
System.gc ();
}
}
public void releaseBitmapCache ()
{
if (imageCache! = null)
{
for (Entry & lt; String, SoftReference & lt; Bitmap & gt; & gt; entry: imageCache
.entrySet ())
{
Bitmap bitmap = entry.getValue () get ();.
if (bitmap! = null)
{
// System.out.println ("bitmap.recycle ()" + bitmap);
// Bitmap.recycle (); // release bitmap objects
bitmap = null;
}
}
// System.out.println ("& gt; & gt; & gt; & gt; & gt; & gt; & gt; & gt; & gt; & gt; & gt; & gt; & gt; & gt; size" + imageCache.size ());
// Set set = imageCache.entrySet ();
// Iterator = set.iterator ();
// While (it.hasNext ())
// {
// SoftReference & lt; Bitmap & gt; reference = (SoftReference & lt; Bitmap & gt;) it.next ();
//
// System.out.println ("& gt; & gt; & gt; & gt; & gt; & gt; & gt; & gt; & gt; & gt; & gt; & gt; & gt; & gt;" + reference.get ());
//// Reference.get () recycle ();.
//}
}
}
// Determine whether there is buffered image, there then take the cache, not on the open-threaded download
public Bitmap loadBitmap (final ImageView imageView, final String imageURL,
final ImageCallBack imageCallBack)
{
// Cached
if (imageCache.containsKey (imageURL))
{
SoftReference & lt; Bitmap & gt; reference = imageCache.get (imageURL);
Bitmap bitmap = reference.get ();
if (bitmap! = null)
{
return bitmap;
}
}
else
{
/ **
* Plus a local cache lookup
* /
String bitmapName = imageURL
.substring (imageURL.lastIndexOf ("/") + 1);
File cacheDir = new File ("/ mnt / sdcard / LoadImgTest /");
File [] cacheFiles = cacheDir.listFiles ();
int i = 0;
if (null! = cacheFiles)
{
for (; i & lt; cacheFiles.length; i ++)
{
if (bitmapName.equals (cacheFiles [i] .getName ()))
{
break;
}
}
if (i & lt; cacheFiles.length)
{
Bitmap bitmap = BitmapFactory
.decodeFile ("/ mnt / sdcard / LoadImgTest /"
+ BitmapName);
return bitmap;
}
}
}
final Handler handler = new Handler ()
{
Override
public void handleMessage (Message msg)
{
imageCallBack.imageLoad (imageView, (Bitmap) msg.obj);
}
};
// If the cache is not in memory, not in the local (by jvm recovered off), then turn on the thread Photo
new Thread ()
{
Override
public void run ()
{
InputStream bitmapIs = HttpUtils.getStreamFromURL (imageURL);
Bitmap bitmap = BitmapFactory.decodeStream (bitmapIs);
if (bitmap == null)
{
return;
}
imageCache.put (imageURL, new SoftReference & lt; Bitmap & gt; (bitmap));
Message msg = handler.obtainMessage (0, bitmap);
handler.sendMessage (msg);
// File dir = new File ("/ mnt / sdcard / ShidaokeImageCash /");
// If (! Dir.exists ())
// {
// Dir.mkdirs ();
//}
if (Environment.getExternalStorageState (). equals (
Environment.MEDIA_MOUNTED))
{// Sdcard
cacheDir = new File (
Environment.getExternalStorageDirectory (),
"LoadImgTest");
}
if (cacheDir = null & amp;! & amp;! cacheDir.exists ())
{
cacheDir.mkdirs ();
}
// If (! Dir.exists ())
// {
// Dir.mkdirs ();
//}
File bitmapFile = new File ("/ mnt / sdcard / LoadImgTest /"
+ ImageURL.substring (imageURL.lastIndexOf ("/") + 1));
if (! bitmapFile.exists ())
{
try
{
bitmapFile.createNewFile ();
}
catch (Exception e)
{
System.out.println ("loadBitmap e" + e.toString ());
}
}
FileOutputStream fos;
try
{
fos = new FileOutputStream (bitmapFile);
bitmap.compress (Bitmap.CompressFormat.JPEG, 100, fos);
fos.close ();
}
catch (Exception e)
{
System.out.println ("loadBitmap exc" + e.toString ());
}
}
} .start ();
return null;
}
public interface ImageCallBack
{
public void imageLoad (ImageView imageView, Bitmap bitmap);
}
public void checkImageCache ()
{
if (Environment.getExternalStorageState (). equals (
Environment.MEDIA_MOUNTED))
{// Sdcard
cacheDir = new File (Environment.getExternalStorageDirectory (),
"LoadImgTest");
}
if (cacheDir.isDirectory ())
{
clearSdcardCache (); // clear the cache data
}
}
/ **
* Clear picture
* /
private static void clearSdcardCache ()
{
File [] files = cacheDir.listFiles ();
long currentTime = System.currentTimeMillis ();
if (files == null)
{
return;
}
for (File file: files)
{
if (currentTime - file.lastModified () & gt; DELETE_TIME_INTERVAL)
{
file.delete ();
continue;
}
}
}
}
2, activity
public class MainActivity extends Activity
{
private ListView listView;
private List & lt; String & gt; imgs = new ArrayList & lt; String & gt; ();
private ImgAdapter adapter;
Override
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
initListview ();
}
private void initListview ()
{
String [] img = {
"Http://b.tengw.cn/upload/min/137/137223191334818540.jpg",
"Http://b.tengw.cn/upload/min/193/13726011398415954.jpg",
"Http://b.tengw.cn/upload/min/158/137271814094517154.jpg",
"Http://b.tengw.cn/upload/min/190/137271767475117154.jpg",
};
for (int i = 0; i & lt; img.length; i ++)
{
imgs.add (img [i]);
}
listView = (ListView) findViewById (R.id.listview);
adapter = new ImgAdapter (getApplication (), imgs);
listView.setAdapter (adapter);
}
}
3,
public class ImgAdapter extends BaseAdapter
{
private Context context;
private List & lt; String & gt; imgs;
private LayoutInflater inflater;
private Bitmap bitmap;
public ImgAdapter (Context context, List & lt; String & gt; imgs)
{
super ();
this.context = context;
this.imgs = imgs;
inflater = LayoutInflater.from (context);
}
Override
public int getCount ()
{
return imgs.size ();
}
Override
public Object getItem (int position)
{
return imgs.get (position);
}
Override
public long getItemId (int position)
{
return position;
}
Override
public View getView (int position, View convertView, ViewGroup parent)
{
ViewHolder vh;
if (convertView == null)
{
vh = new ViewHolder ();
convertView = inflater.inflate (R.layout.item, null);
vh.img = (ImageView) convertView.findViewById (R.id.img);
convertView.setTag (vh);
}
else
{
vh = (ViewHolder) convertView.getTag ();
}
bitmap = new AsyncBitmapLoader (). loadBitmap (vh.img, imgs.get (position),
new ImageCallBack ()
{
Override
public void imageLoad (ImageView imageView, Bitmap bitmap)
{
imageView.setImageBitmap (bitmap);
}
});
if (bitmap == null)
{
vh.img.setImageBitmap (null);
}
else
{
vh.img.setImageBitmap (bitmap);
}
return convertView;
}
private class ViewHolder
{
ImageView img;
}
}
Reply:
4,
/ **
* This is a good package loaded asynchronously picture category, two cache,
* Use a soft references are cached in memory,
* A is cached locally sd card,
* If there is no memory, from the local to find, if not from the local network get the picture.
*author Tengwang-sl
*
* /
public class HttpUtils
{
public static InputStream getStreamFromURL (String imageURL)
{
InputStream in = null;
try
{
URL url = new URL (imageURL);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection ();
in = connection.getInputStream ();
}
catch (Exception e)
{
}
return in;
}
}
5, the layout file
& lt; RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
xmlns: tools = "http://schemas.android.com/tools"
android: layout_width = "match_parent"
android: layout_height = "match_parent" & gt;
& Lt; ListView
android: id = "@ + id / listview"
android: layout_width = "match_parent"
android: layout_height = "wrap_content"
android: text = "@ string / hello_world" / & gt;
& Lt; / RelativeLayout & gt;
??
& lt; xml version = "1.0" encoding = "utf-8" & gt;
& Lt; LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: background = "@ drawable / img_bg"
android: orientation = "vertical" & gt;
& Lt; ImageView
android: id = "@ + id / img"
android: layout_width = "200dp"
android: layout_height = "200dp" / & gt;
& Lt; / LinearLayout & gt;
Reply:
6, authority
& lt; uses-permission android: name = "android.permission.INTERNET" / & gt;
& Lt; uses-permission android: name = "android.permission.WRITE_EXTERNAL_STORAGE" / & gt;
& Lt; uses-permission android: name = "android.permission.READ_EXTERNAL_STORAGE" / & gt;
& Lt; uses-permission android: name = "android.permission.MOUNT_UNMOUNT_FILESYSTEMS" / & gt;
Reply:
Their top one, no one to restore it
Reply:
The code did not look too long, from your description of view, you can see if it is not a memory overflow. In the big picture solution inside android often overflow, can be judged by the size of the picture, and then set Options.inSampleSize resolved, there is described above developer.android.com
Reply:
I can always find a resolution of relatively small picture does not show up, ah, a big show out
Reply:
I can always find a resolution of relatively small picture does not show up, ah, a big show out
Reply:
Reply:
You change the location to the picture not know and have no relationship resolution
Reply:
Thanks for sharing. Thank landlord
No comments:
Post a Comment