SHARE
TWEET

External storage+AsyncTask

a guest Feb 18th, 2019 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.e.asynctask;
  2.  
  3. import android.Manifest;
  4. import android.app.ProgressDialog;
  5. import android.content.pm.PackageManager;
  6. import android.graphics.drawable.Drawable;
  7. import android.os.AsyncTask;
  8. import android.os.Build;
  9. import android.os.Environment;
  10. import android.support.annotation.NonNull;
  11. import android.support.v7.app.AppCompatActivity;
  12. import android.os.Bundle;
  13. import android.view.View;
  14. import android.widget.Button;
  15. import android.widget.ImageView;
  16. import android.widget.Toast;
  17.  
  18. import java.io.BufferedInputStream;
  19. import java.io.BufferedOutputStream;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import java.net.HttpURLConnection;
  23. import java.net.MalformedURLException;
  24. import java.net.URL;
  25. import java.security.Permission;
  26.  
  27. import javax.net.ssl.HttpsURLConnection;
  28.  
  29. public class MainActivity extends AppCompatActivity {
  30.  
  31.     int REQUEST_WRITE_EXTERNAL_STORAGE = 1;
  32.     ImageView image;
  33.     final String url = "http://www.thebigideatoolkit.com/wp-content/uploads/2011/07/Big-Picture-example.jpg";
  34.  
  35.     @Override
  36.     protected void onCreate(Bundle savedInstanceState) {
  37.         super.onCreate(savedInstanceState);
  38.         setContentView(R.layout.activity_main);
  39.  
  40.         image = findViewById(R.id.image_iv);
  41.         Button download = findViewById(R.id.download_btn);
  42.         download.setOnClickListener(new View.OnClickListener() {
  43.             @Override
  44.             public void onClick(View v) {
  45.                 if(Build.VERSION.SDK_INT >=23){
  46.                     int ifperrmission = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
  47.                     if(ifperrmission != PackageManager.PERMISSION_GRANTED) {
  48.                         requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_WRITE_EXTERNAL_STORAGE);
  49.                     }
  50.                     else
  51.                     {
  52.                         new DownloadImage().execute(url);
  53.                     }
  54.                 }
  55.  
  56.             }
  57.         });
  58.     }
  59.  
  60.     @Override
  61.     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  62.         super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  63.         if(requestCode == REQUEST_WRITE_EXTERNAL_STORAGE){
  64.             if(grantResults[0] != PackageManager.PERMISSION_GRANTED){
  65.                 Toast.makeText(this, "Need permission", Toast.LENGTH_SHORT).show();
  66.             }
  67.             else{
  68.                 new DownloadImage().execute(url);
  69.  
  70.             }
  71.         }
  72.     }
  73.     private class DownloadImage extends AsyncTask<String,Integer,String>{
  74.     ProgressDialog progressdialog;
  75.  
  76.         @Override
  77.         protected void onPreExecute() {
  78.             super.onPreExecute();
  79.  
  80.             image.setImageBitmap(null);
  81.             progressdialog = new ProgressDialog(MainActivity.this);
  82.             progressdialog.setTitle("Please wait");
  83.             progressdialog.setMessage("Downloading image");
  84.             progressdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  85.             progressdialog.show();
  86.         }
  87.  
  88.         @Override
  89.         protected void onPostExecute(String s) {
  90.             super.onPostExecute(s);
  91.             image.setImageDrawable(Drawable.createFromPath(s));
  92.             progressdialog.dismiss();
  93.         }
  94.  
  95.         @Override
  96.         protected void onProgressUpdate(Integer... values) {
  97.             super.onProgressUpdate(values);
  98.             progressdialog.setProgress(values[0]);
  99.         }
  100.  
  101.         @Override
  102.         protected String doInBackground(String... strings) {
  103.             String outputfilepath = null;
  104.  
  105.             try {
  106.                 URL url = new URL(strings[0]);
  107.                 HttpURLConnection httpurlconnection = (HttpURLConnection)url.openConnection();
  108.                 httpurlconnection.connect();
  109.  
  110.                 long lens_of_file = httpurlconnection.getContentLength();
  111.                 BufferedInputStream bis = new BufferedInputStream(httpurlconnection.getInputStream());
  112.                 outputfilepath = Environment.getExternalStorageDirectory().toString() + "/piccc.jpg";
  113.                 BufferedOutputStream fos =new BufferedOutputStream(new FileOutputStream(outputfilepath));
  114.  
  115.                 final int BUFFER_SIZE = 4096;
  116.                 byte[] buffer = new byte[BUFFER_SIZE];
  117.  
  118.                 int current;
  119.                 int total = 0;
  120.  
  121.                 while ((current = bis.read(buffer)) != -1) {
  122.                     total += current;
  123.                     publishProgress((int) (total*100/lens_of_file));
  124.                     fos.write(buffer,0,current);
  125.                 }
  126.                 bis.close();
  127.                 fos.close();
  128.  
  129.             } catch (MalformedURLException e) {
  130.                 e.printStackTrace();
  131.             } catch (IOException e) {
  132.                 e.printStackTrace();
  133.             }
  134.             return outputfilepath;
  135.         }
  136.     }
  137. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top