これはあなたが望むものの完全な例です..
AsyncOncImageActivity.java
public class AsyncOncImageActivity extends Activity {
/** Called when the activity is first created. */
ProgressBar pbHorizontal;
ImageView ivImage;
//Button btnIncrement;
String URL="http://appcolumn5.columnfivemedia.netdna-cdn.com/wp-content/uploads/2012/03/TakePart-Infographic_MarijuanaAttitudesandLegislation.png";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*** FIND ALL COMPONENT ***/
ivImage = (ImageView) findViewById(R.id.ivImage);
pbHorizontal=(ProgressBar) findViewById(R.id.pbHorizontal);
//btnIncrement=(Button) findViewById(R.id.btnIncrement);
/*** DOWNLOAD IMAG FROM URL AND READ FROM SD CARD***/
new AsyncDemo(URL,pbHorizontal,ivImage).execute(this);
}
class AsyncDemo extends AsyncTask<Context, ProgressBar, Bitmap>
{
String url;
Boolean cancelTask=true;
ProgressBar pbHorizontal;
ImageView ivImage;
AsyncDemo(String url,ProgressBar pbHorizontal,ImageView ivImage)
{
this.url = url;
this.pbHorizontal=pbHorizontal;
this.ivImage=ivImage;
}
@Override
protected Bitmap doInBackground(Context... params) {
// TODO Auto-generated method stub
try {
File root = Environment.getExternalStorageDirectory();
URL u = new URL(url);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
FileOutputStream f = new FileOutputStream(new File(root, "c5.jpg"));
InputStream in = c.getInputStream();
// here u will get content length (Size of image)
System.out.println("Content length"+c.getContentLength());
//pbHorizontal.setMax(c.getContentLength());
byte[] buffer = new byte[1024];
int len1 = 0;
/*** WRITTING IMAGE TO SD CARD **/
while ((len1 = in.read(buffer)) > 0)// && cancelTask==true)
{
f.write(buffer, 0, len1);
System.out.println("current buf: "+len1);
//pbHorizontal.incrementProgressBy(len1);
pbHorizontal.setProgress(pbHorizontal.getProgress()+len1);
//System.out.println("status Loop:"+this.getStatus());
}
f.close();// image stored in sd card
/*** READING IMAGE FROM SD CARD ***/
String imageInSD = "/sdcard/c5.jpg";
// System.out.println("statusf inis:"+this.getStatus());
Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
return bitmap;
} catch (Exception e) {
Log.d("Downloader", e.getMessage());
return null;
}
}
@Override
protected void onPostExecute(Bitmap result )
{
ivImage.setImageBitmap(result);
pbHorizontal.setVisibility(ProgressBar.GONE);
}
protected void stopIt()
{
cancelTask=false;
}
}
}
main.xml
<ImageView
android:id="@+id/ivImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" />
<ProgressBar
android:id="@+id/pbHorizontal"
android:progress="0"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp" />
</RelativeLayout>