Advertisement
gromdroid

ImageLoader

May 9th, 2013
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 KB | None | 0 0
  1.  /** AsyncTask to download and load an image in ListView */
  2.     private class ImageLoaderTask extends AsyncTask<HashMap<String, Object>, Void, HashMap<String, Object>>{
  3.  
  4.         @Override
  5.         protected HashMap<String, Object> doInBackground(HashMap<String, Object>... hm) {
  6.            
  7.             InputStream iStream=null;
  8.             String imgUrl = (String) hm[0].get("flag_path");
  9.             int position = (Integer) hm[0].get("position");
  10.            
  11.             URL url;
  12.             try {
  13.                 url = new URL(imgUrl);
  14.                
  15.                 // Creating an http connection to communicate with url
  16.                 HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
  17.  
  18.                 // Connecting to url               
  19.                 urlConnection.connect();
  20.  
  21.                 // Reading data from url
  22.                 iStream = urlConnection.getInputStream();
  23.                
  24.                 // Getting Caching directory
  25.                 File cacheDirectory = getBaseContext().getCacheDir();
  26.                
  27.                 // Temporary file to store the downloaded image
  28.                 File tmpFile = new File(cacheDirectory.getPath() + "/wpta_"+position+".jpeg");             
  29.                    
  30.                 // The FileOutputStream to the temporary file
  31.                 FileOutputStream fOutStream = new FileOutputStream(tmpFile);
  32.                
  33.                 // Creating a bitmap from the downloaded inputstream
  34.                 Bitmap b = BitmapFactory.decodeStream(iStream);            
  35.                
  36.                 // Writing the bitmap to the temporary file as png file
  37.                 b.compress(Bitmap.CompressFormat.JPEG, 100, fOutStream);               
  38.                
  39.                 // Flush the FileOutputStream
  40.                 fOutStream.flush();
  41.                
  42.                 //Close the FileOutputStream
  43.                 fOutStream.close();            
  44.                
  45.                 // Create a hashmap object to store image path and its position in the listview
  46.                 HashMap<String, Object> hmBitmap = new HashMap<String, Object>();
  47.                
  48.                 // Storing the path to the temporary image file
  49.                 hmBitmap.put("flag",tmpFile.getPath());
  50.                
  51.                 // Storing the position of the image in the listview
  52.                 hmBitmap.put("position",position);             
  53.                
  54.                 // Returning the HashMap object containing the image path and position
  55.                 return hmBitmap;               
  56.                
  57.  
  58.             }catch (Exception e) {             
  59.                 e.printStackTrace();
  60.             }
  61.             return null;
  62.         }
  63.        
  64.         @Override
  65.         protected void onPostExecute(HashMap<String, Object> result) {
  66.             // Getting the path to the downloaded image
  67.             String path = (String) result.get("flag"); 
  68.             String medium = path.replace("small", "medium");
  69.            
  70.             // Getting the position of the downloaded image
  71.             int position = (Integer) result.get("position");
  72.            
  73.             // Getting adapter of the listview
  74.             SimpleAdapter adapter = (SimpleAdapter ) mListView.getAdapter();
  75.            
  76.             // Getting the hashmap object at the specified position of the listview
  77.             HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(position);  
  78.            
  79.             // Overwriting the existing path in the adapter
  80.            
  81.             hm.put("flag",medium);
  82.             // Noticing listview about the dataset changes
  83.             adapter.notifyDataSetChanged();
  84.             setProgressBarIndeterminateVisibility(false);
  85.         }
  86.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement