2013年9月24日 星期二

Android Progress Bar Example (part 2)

Android Application Activity - MainActivity.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.as400samplecode;
 
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
 
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.BitmapFactory;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
 
public class MainActivity extends Activity implements OnClickListener{
 
 private ProgressBar progressBar1;
 private ProgressBar progressBar2;
 private String filepath = "MyFileStorage";
 private File directory;
 private TextView finished;
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
        directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
   
        progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
        progressBar1.setVisibility(View.GONE);
        progressBar2 = (ProgressBar) findViewById(R.id.progressBar2);
        progressBar2.setVisibility(View.GONE);
        finished = (TextView) findViewById(R.id.textView1);
        finished.setVisibility(View.GONE);
         
        Button start = (Button) findViewById(R.id.start);
  start.setOnClickListener(this);
  Button stop = (Button) findViewById(R.id.stop);
  stop.setOnClickListener(this);
  Button download = (Button) findViewById(R.id.download);
  download.setOnClickListener(this);
         
    }
     
    public void onClick(View v) {
 
  switch (v.getId()) {
  case R.id.start:
         progressBar1.setVisibility(View.VISIBLE);
   break;
    
  case R.id.stop:
         progressBar1.setVisibility(View.GONE);
   break;
    
  case R.id.download:
   grabURL(url);
   break;
    
   // More buttons go here (if any) ...
 
  }
 }
     
    public void grabURL(String url) {
  new GrabURL().execute(url);
 }
  
 private class GrabURL extends AsyncTask<String, Integer, String> {
   
   
  protected void onPreExecute() {
   progressBar2.setVisibility(View.VISIBLE);
         progressBar2.setProgress(0);
         finished.setVisibility(View.GONE);
          
     }
 
  protected String doInBackground(String... urls) {
 
   String filename = "MySampleFile.png";
   File myFile = new File(directory , filename);
    
   try {
             URL url = new URL(urls[0]);
             URLConnection connection = url.openConnection();
             connection.connect();
             int fileSize = connection.getContentLength();
 
             InputStream is = new BufferedInputStream(url.openStream());
             OutputStream os = new FileOutputStream(myFile);
 
             byte data[] = new byte[1024];
             long total = 0;
             int count;
             while ((count = is.read(data)) != -1) {
                 total += count;
                 publishProgress((int) (total * 100 / fileSize));
                 os.write(data, 0, count);
             }
 
             os.flush();
             os.close();
             is.close();
              
         } catch (Exception e) {
          e.printStackTrace();
         }
 
   return filename;
    
  }
 
  protected void onProgressUpdate(Integer... progress) {
   finished.setVisibility(View.VISIBLE);
   finished.setText(String.valueOf(progress[0]) + "%");
   progressBar2.setProgress(progress[0]);
     }
   
  protected void onCancelled() {
   Toast toast = Toast.makeText(getBaseContext(),
     "Error connecting to Server", Toast.LENGTH_LONG);
   toast.setGravity(Gravity.TOP, 25, 400);
   toast.show();
 
  }
 
  protected void onPostExecute(String filename) {
   progressBar2.setProgress(100);
   finished.setVisibility(View.VISIBLE);
   finished.setText("Finished downloading...");
   File myFile = new File(directory , filename);
   ImageView myImage = (ImageView) findViewById(R.id.imageView1);
   myImage.setImageBitmap(BitmapFactory.decodeFile(myFile.getAbsolutePath()));
  }
 
 }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

Related Posts Plugin for WordPress, Blogger...