<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="200dp" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:scaleType="fitCenter"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/imageView" android:layout_centerHorizontal="true" android:layout_marginTop="41dp" android:text="从网络上下载一张图片" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xiaoluo.android_asynctast" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <!-- 授权手机能够访问网络 --> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.xiaoluo.android_asynctast.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
public class MainActivity extends Activity
{
private Button button;
private ImageView imageView;
private ProgressDialog progressDialog;
private final String IMAGE_PATH = "http://developer.android.com/images/home/kk-hero.jpg";
// private final String IMAGE_PATH2 = "http://ww2.sinaimg.cn/mw690/69c7e018jw1e6hd0vm3pej20fa0a674c.jpg";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
imageView = (ImageView)findViewById(R.id.imageView);
// 弹出要给ProgressDialog
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("提示信息");
progressDialog.setMessage("正在下载中,请稍后......");
// 设置setCancelable(false); 表示我们不能取消这个弹出框,等下载完成之后再让弹出框消失
progressDialog.setCancelable(false);
// 设置ProgressDialog样式为圆圈的形式
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// 在UI Thread当中实例化AsyncTask对象,并调用execute方法
new MyAsyncTask().execute(IMAGE_PATH);
}
});
}
/**
* 定义一个类,让其继承AsyncTask这个类
* Params: String类型,表示传递给异步任务的参数类型是String,通常指定的是URL路径
* Progress: Integer类型,进度条的单位通常都是Integer类型
* Result:byte[]类型,表示我们下载好的图片以字节数组返回
* @author xiaoluo
*
*/
public class MyAsyncTask extends AsyncTask<String, Integer, byte[]>
{
@Override
protected void onPreExecute()
{
super.onPreExecute();
// 在onPreExecute()中我们让ProgressDialog显示出来
progressDialog.show();
}
@Override
protected byte[] doInBackground(String... params)
{
// 通过Apache的HttpClient来访问请求网络中的一张图片
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(params[0]);
byte[] image = new byte[]{};
try
{
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
if(httpEntity != null && httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
image = EntityUtils.toByteArray(httpEntity);
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
httpClient.getConnectionManager().shutdown();
}
return image;
}
@Override
protected void onProgressUpdate(Integer... values)
{
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(byte[] result)
{
super.onPostExecute(result);
// 将doInBackground方法返回的byte[]解码成要给Bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(result, 0, result.length);
// 更新我们的ImageView控件
imageView.setImageBitmap(bitmap);
// 使ProgressDialog框消失
progressDialog.dismiss();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
public class MainActivity extends Activity
{
private Button button;
private ImageView imageView;
private ProgressDialog progressDialog;
private final String IMAGE_PATH = "http://developer.android.com/images/home/kk-hero.jpg";
// private final String IMAGE_PATH2 = "http://ww2.sinaimg.cn/mw690/69c7e018jw1e6hd0vm3pej20fa0a674c.jpg";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
imageView = (ImageView)findViewById(R.id.imageView);
// 弹出要给ProgressDialog
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("提示信息");
progressDialog.setMessage("正在下载中,请稍后......");
// 设置setCancelable(false); 表示我们不能取消这个弹出框,等下载完成之后再让弹出框消失
progressDialog.setCancelable(false);
// 设置ProgressDialog样式为水平的样式
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
new MyAsyncTask().execute(IMAGE_PATH);
}
});
}
/**
* 定义一个类,让其继承AsyncTask这个类
* Params: String类型,表示传递给异步任务的参数类型是String,通常指定的是URL路径
* Progress: Integer类型,进度条的单位通常都是Integer类型
* Result:byte[]类型,表示我们下载好的图片以字节数组返回
* @author xiaoluo
*
*/
public class MyAsyncTask extends AsyncTask<String, Integer, byte[]>
{
@Override
protected void onPreExecute()
{
super.onPreExecute();
// 在onPreExecute()中我们让ProgressDialog显示出来
progressDialog.show();
}
@Override
protected byte[] doInBackground(String... params)
{
// 通过Apache的HttpClient来访问请求网络中的一张图片
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(params[0]);
byte[] image = new byte[]{};
try
{
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream inputStream = null;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
if(httpEntity != null && httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
// 得到文件的总长度
long file_length = httpEntity.getContentLength();
// 每次读取后累加的长度
long total_length = 0;
int length = 0;
// 每次读取1024个字节
byte[] data = new byte[1024];
inputStream = httpEntity.getContent();
while(-1 != (length = inputStream.read(data)))
{
// 每读一次,就将total_length累加起来
total_length += length;
// 边读边写到ByteArrayOutputStream当中
byteArrayOutputStream.write(data, 0, length);
// 得到当前图片下载的进度
int progress = ((int)(total_length/(float)file_length) * 100);
// 时刻将当前进度更新给onProgressUpdate方法
publishProgress(progress);
}
}
image = byteArrayOutputStream.toByteArray();
inputStream.close();
byteArrayOutputStream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
httpClient.getConnectionManager().shutdown();
}
return image;
}
@Override
protected void onProgressUpdate(Integer... values)
{
super.onProgressUpdate(values);
// 更新ProgressDialog的进度条
progressDialog.setProgress(values[0]);
}
@Override
protected void onPostExecute(byte[] result)
{
super.onPostExecute(result);
// 将doInBackground方法返回的byte[]解码成要给Bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(result, 0, result.length);
// 更新我们的ImageView控件
imageView.setImageBitmap(bitmap);
// 使ProgressDialog框消失
progressDialog.dismiss();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有