Advertisement
Guest User

Untitled

a guest
Apr 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.03 KB | None | 0 0
  1.  private class DownloadTask extends AsyncTask<URL,Void,Bitmap>{
  2.         // Before the tasks execution
  3.         protected void onPreExecute(){
  4.             // Display the progress dialog on async task start
  5.             mProgressDialog.show();
  6.         }
  7.  
  8.         // Do the task in background/non UI thread
  9.         protected Bitmap doInBackground(URL...urls){
  10.             URL url = urls[0];
  11.             HttpURLConnection connection = null;
  12.  
  13.             try{
  14.                 // Initialize a new http url connection
  15.                 connection = (HttpURLConnection) url.openConnection();
  16.  
  17.                 // Connect the http url connection
  18.                 connection.connect();
  19.  
  20.                 // Get the input stream from http url connection
  21.                 InputStream inputStream = connection.getInputStream();
  22.  
  23.                 /*
  24.                     BufferedInputStream
  25.                         A BufferedInputStream adds functionality to another input stream-namely,
  26.                         the ability to buffer the input and to support the mark and reset methods.
  27.                 */
  28.                 /*
  29.                     BufferedInputStream(InputStream in)
  30.                         Creates a BufferedInputStream and saves its argument,
  31.                         the input stream in, for later use.
  32.                 */
  33.                 // Initialize a new BufferedInputStream from InputStream
  34.                 BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
  35.  
  36.                 /*
  37.                     decodeStream
  38.                         Bitmap decodeStream (InputStream is)
  39.                             Decode an input stream into a bitmap. If the input stream is null, or
  40.                             cannot be used to decode a bitmap, the function returns null. The stream's
  41.                             position will be where ever it was after the encoded data was read.
  42.  
  43.                         Parameters
  44.                             is InputStream : The input stream that holds the raw data
  45.                                               to be decoded into a bitmap.
  46.                         Returns
  47.                             Bitmap : The decoded bitmap, or null if the image data could not be decoded.
  48.                 */
  49.                 // Convert BufferedInputStream to Bitmap object
  50.                 Bitmap bmp = BitmapFactory.decodeStream(bufferedInputStream);
  51.  
  52.                 // Return the downloaded bitmap
  53.                 return bmp;
  54.  
  55.             }catch(IOException e){
  56.                 e.printStackTrace();
  57.             }finally{
  58.                 // Disconnect the http url connection
  59.                 connection.disconnect();
  60.             }
  61.             return null;
  62.         }
  63.  
  64.         // When all async task done
  65.         protected void onPostExecute(Bitmap result){
  66.             // Hide the progress dialog
  67.             mProgressDialog.dismiss();
  68.  
  69.             if(result!=null){
  70.                 // Display the downloaded image into ImageView
  71.                 mImageView.setImageBitmap(result);
  72.  
  73.                 // Save bitmap to internal storage
  74.                 Uri imageInternalUri = saveImageToInternalStorage(result);
  75.                 // Set the ImageView image from internal storage
  76.                 mImageViewInternal.setImageURI(imageInternalUri);
  77.             }else {
  78.                 // Notify user that an error occurred while downloading image
  79.                 Snackbar.make(mCLayout,"Error",Snackbar.LENGTH_LONG).show();
  80.             }
  81.         }
  82.     }
  83.  
  84.     // Custom method to convert string to url
  85.     protected URL stringToURL(String urlString){
  86.         try{
  87.             URL url = new URL(urlString);
  88.             return url;
  89.         }catch(MalformedURLException e){
  90.             e.printStackTrace();
  91.         }
  92.         return null;
  93.     }
  94.  
  95.     // Custom method to save a bitmap into internal storage
  96.     protected Uri saveImageToInternalStorage(Bitmap bitmap){
  97.         // Initialize ContextWrapper
  98.         ContextWrapper wrapper = new ContextWrapper(getApplicationContext());
  99.  
  100.         // Initializing a new file
  101.         // The bellow line return a directory in internal storage
  102.         File file = wrapper.getDir("Images",MODE_PRIVATE);
  103.  
  104.         // Create a file to save the image
  105.         file = new File(file, "UniqueFileName"+".jpg");
  106.  
  107.         try{
  108.             // Initialize a new OutputStream
  109.             OutputStream stream = null;
  110.  
  111.             // If the output file exists, it can be replaced or appended to it
  112.             stream = new FileOutputStream(file);
  113.  
  114.             // Compress the bitmap
  115.             bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream);
  116.  
  117.             // Flushes the stream
  118.             stream.flush();
  119.  
  120.             // Closes the stream
  121.             stream.close();
  122.  
  123.         }catch (IOException e) // Catch the exception
  124.         {
  125.             e.printStackTrace();
  126.         }
  127.  
  128.         // Parse the gallery image url to uri
  129.         Uri savedImageURI = Uri.parse(file.getAbsolutePath());
  130.  
  131.         // Return the saved image Uri
  132.         return savedImageURI;
  133.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement