Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GurDown extends Fragment {
- Button btn;
- ImageView my_image;
- String url = "https://media.alienwarearena.com/media/1327-a.jpg";
- ProgressDialog pDialog;
- public static final int progress_bar_type = 0;
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- return inflater.inflate(R.layout.gur_down, container, false);
- }
- @Override
- public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- getActivity().setTitle("GurDown");
- btn = (Button) view.findViewById(R.id.button);
- my_image = (ImageView) view.findViewById(R.id.imageDisplay);
- btn.setOnClickListener(new View.OnClickListener(){
- @Override
- public void onClick(View v) {
- new DownloadFileFromURL().execute(url);
- // new Downloader().execute(url);
- }
- });
- }
- /*
- class Downloader extends AsyncTask<String,Void,Bitmap> {
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- pDialog = new ProgressDialog(getActivity());
- pDialog.setTitle("Image Downloader");
- pDialog.setMessage("Downloading. . .");
- pDialog.setIndeterminate(false);
- pDialog.show();
- }
- @Override
- protected Bitmap doInBackground(String... url) {
- String myurl = url[0];
- Bitmap bm=null;
- try {
- InputStream is=new URL(myurl).openStream();
- bm=BitmapFactory.decodeStream(is);
- } catch(Exception e) {
- e.printStackTrace();
- }
- return bm;
- }
- @Override
- protected void onPostExecute(Bitmap result) {
- super.onPostExecute(result);
- my_image.setImageBitmap(result);
- pDialog.dismiss();
- }
- }*/
- class DownloadFileFromURL extends AsyncTask<String, String, String> {
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- // pDialog = ProgressDialog.show(getActivity(), "Loading...", "Please wait...", true);
- pDialog = new ProgressDialog(getActivity());
- pDialog.setMessage("Downloading file. Please wait. . .");
- pDialog.setIndeterminate(false);
- pDialog.setMax(100);
- pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
- pDialog.setCancelable(true);
- pDialog.show();
- }
- @Override
- protected String doInBackground(String... f_url) {
- int count;
- try {
- URL url = new URL(f_url[0]);
- URLConnection urlConection = url.openConnection();
- urlConection.connect();
- int lengthOfFile = urlConection.getContentLength();
- InputStream input = new BufferedInputStream(url.openStream(), 8192);
- String storageDir = Environment.getExternalStorageDirectory().getAbsolutePath();
- String fileName = "/file.jpg";
- File imageFile = new File(storageDir+fileName);
- OutputStream output = new FileOutputStream(imageFile);
- byte data[] = new byte[1024];
- long total = 0;
- while((count = input.read(data)) != -1) {
- total += count;
- publishProgress(""+(int)((total*100)/lengthOfFile));
- output.write(data, 0, count);
- }
- output.flush();
- output.close();
- input.close();
- } catch (Exception e) {
- Log.e("Error: ", e.getMessage());
- }
- return null;
- }
- protected void onProgressUpdate(String... progress) {
- pDialog.setProgress(Integer.parseInt(progress[0]));
- }
- protected void onPostExecute(String file_url) {
- pDialog.dismiss();
- Toast.makeText(getActivity(),"done!", Toast.LENGTH_LONG).show();
- String imagePath = Environment.getExternalStorageDirectory() + "/file1.jpg";
- my_image.setImageDrawable(Drawable.createFromPath(imagePath));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement