bitmap - android picture download -
I return a bitmap object according to a URL, and download photo code:
< Code> url = new URL (image URLSTR); URL Connection Connection = url.openConnection (); Conn.setDoInput (true); Conn.connect (); InputStream = conn.getInputStream (); Bitmap bitmap = bitmapfenderDecodedStream (in); In.closeThen I save it to sdcard. OK to save the image
The problem now is that Image A is being downloaded to access this URL. But it now shows another and B photo in the SDCard. How to solve this problem ¼ ??
You can identify images by hash codes. Not a good solution for the demo,
Private bitmap getBitmap (string URL) {string filename = string.value (); File f = new file (cache directory, filename); // bitmap b = decodfile (s) from SD cache; If (b! = Null) return b; // Try the Web {bitmap bitmap = null; InputStream = New URL (url) .openstream (); OutputStream OS = New FileOutputStream (f); CopyStream (is, OS); Os.close (); Bitmap = decodefile (f); Return bitmap; } Hold (Exception pre) {ex.printStackTrace (); Return tap; }} Private Zero copystream (InputStream, OutputStream OS) {Final int buffer_size = 1024; Try {byte [] bytes = new byte [buffer_size]; For (;;) {int count = is.read (bytes, 0, buffer_size); Break (calculation == -1); Os.write (bytes, 0, count); }} Hold (Exception Ex) {}} / ** Decode Image and Scale to reduce memory consumption / Private bitmap decodefile (file F) {try {BitmapFactory.Options options = new BitmapFactory.Options (); Options.inPurgeable = true; BitmapFactory.decodeStream return (new FileInputStream (f)); } Hold (FileNotFoundException E) {} return tap; }
You only need to use the "getBitmap (string)" method with your desired url in the form of a string
Comments
Post a Comment