Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. public class GurDown extends Fragment {
  2. Button btn;
  3. ImageView my_image;
  4. String url = "https://media.alienwarearena.com/media/1327-a.jpg";
  5. ProgressDialog pDialog;
  6.  
  7. public static final int progress_bar_type = 0;
  8.  
  9.  
  10.  
  11. @Nullable
  12. @Override
  13. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  14. return inflater.inflate(R.layout.gur_down, container, false);
  15. }
  16.  
  17.  
  18. @Override
  19. public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
  20.  
  21. super.onViewCreated(view, savedInstanceState);
  22.  
  23. getActivity().setTitle("GurDown");
  24.  
  25.  
  26.  
  27. btn = (Button) view.findViewById(R.id.button);
  28. my_image = (ImageView) view.findViewById(R.id.imageDisplay);
  29.  
  30. btn.setOnClickListener(new View.OnClickListener(){
  31. @Override
  32. public void onClick(View v) {
  33. new DownloadFileFromURL().execute(url);
  34. // new Downloader().execute(url);
  35. }
  36.  
  37. });
  38.  
  39.  
  40. }
  41.  
  42. /*
  43. class Downloader extends AsyncTask<String,Void,Bitmap> {
  44.  
  45. @Override
  46. protected void onPreExecute() {
  47. super.onPreExecute();
  48.  
  49. pDialog = new ProgressDialog(getActivity());
  50. pDialog.setTitle("Image Downloader");
  51. pDialog.setMessage("Downloading. . .");
  52. pDialog.setIndeterminate(false);
  53. pDialog.show();
  54. }
  55.  
  56. @Override
  57. protected Bitmap doInBackground(String... url) {
  58. String myurl = url[0];
  59. Bitmap bm=null;
  60.  
  61. try {
  62. InputStream is=new URL(myurl).openStream();
  63. bm=BitmapFactory.decodeStream(is);
  64.  
  65. } catch(Exception e) {
  66. e.printStackTrace();
  67. }
  68.  
  69.  
  70. return bm;
  71. }
  72.  
  73. @Override
  74. protected void onPostExecute(Bitmap result) {
  75. super.onPostExecute(result);
  76.  
  77. my_image.setImageBitmap(result);
  78.  
  79. pDialog.dismiss();
  80. }
  81. }*/
  82.  
  83.  
  84.  
  85. class DownloadFileFromURL extends AsyncTask<String, String, String> {
  86.  
  87. @Override
  88. protected void onPreExecute() {
  89. super.onPreExecute();
  90. // pDialog = ProgressDialog.show(getActivity(), "Loading...", "Please wait...", true);
  91. pDialog = new ProgressDialog(getActivity());
  92. pDialog.setMessage("Downloading file. Please wait. . .");
  93. pDialog.setIndeterminate(false);
  94. pDialog.setMax(100);
  95. pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  96. pDialog.setCancelable(true);
  97. pDialog.show();
  98.  
  99. }
  100.  
  101. @Override
  102. protected String doInBackground(String... f_url) {
  103. int count;
  104. try {
  105. URL url = new URL(f_url[0]);
  106. URLConnection urlConection = url.openConnection();
  107. urlConection.connect();
  108.  
  109. int lengthOfFile = urlConection.getContentLength();
  110.  
  111. InputStream input = new BufferedInputStream(url.openStream(), 8192);
  112.  
  113. String storageDir = Environment.getExternalStorageDirectory().getAbsolutePath();
  114. String fileName = "/file.jpg";
  115. File imageFile = new File(storageDir+fileName);
  116. OutputStream output = new FileOutputStream(imageFile);
  117.  
  118. byte data[] = new byte[1024];
  119. long total = 0;
  120.  
  121. while((count = input.read(data)) != -1) {
  122. total += count;
  123.  
  124. publishProgress(""+(int)((total*100)/lengthOfFile));
  125.  
  126. output.write(data, 0, count);
  127. }
  128.  
  129. output.flush();
  130.  
  131. output.close();
  132. input.close();
  133. } catch (Exception e) {
  134. Log.e("Error: ", e.getMessage());
  135. }
  136. return null;
  137. }
  138.  
  139. protected void onProgressUpdate(String... progress) {
  140. pDialog.setProgress(Integer.parseInt(progress[0]));
  141. }
  142.  
  143. protected void onPostExecute(String file_url) {
  144. pDialog.dismiss();
  145. Toast.makeText(getActivity(),"done!", Toast.LENGTH_LONG).show();
  146.  
  147. String imagePath = Environment.getExternalStorageDirectory() + "/file1.jpg";
  148. my_image.setImageDrawable(Drawable.createFromPath(imagePath));
  149.  
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement