84 Android Hnadler 封装下载图片工具类
2014-01-19 16:03
453 查看
activity_main.xml
DownLoadContent.java
MainActivity.java
package com.example.android_handler_callback;
import com.example.android_handler_callback.DownLoadContent.DownLoadCallBack;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private final String img_path="http://www.baidu.com/img/bdlogo.gif";
private Button button;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button1);
imageView=(ImageView)this.findViewById(R.id.imageView1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
DownLoadContent downLoadContent=new DownLoadContent(MainActivity.this);
downLoadContent.downLoad(img_path, new DownLoadCallBack() {
@Override
public void LoadContetn(byte[] result) {
// TODO Auto-generated method stub
Bitmap bitmap=BitmapFactory.decodeByteArray(result, 0, result.length);
imageView.setImageBitmap(bitmap);
}
});
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
AndroidManifest.xml 在清单文件中添加网络权限
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="42dp" android:layout_marginRight="84dp" android:text="Button" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_alignParentTop="true" android:layout_marginTop="117dp" android:src="@drawable/ic_launcher" /> </RelativeLayout>
DownLoadContent.java
package com.example.android_handler_callback; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import android.app.ProgressDialog; import android.content.Context; import android.os.Handler; import android.os.Message; /** * 工具类 * @author Administrator * */ public class DownLoadContent { private ProgressDialog dialog; public DownLoadContent( Context context) { // TODO Auto-generated constructor stub dialog=new ProgressDialog(context); dialog.setTitle("提示"); dialog.setMessage("正在获取网络数据..."); } public void downLoad(final String path,final DownLoadCallBack callBack){ final Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); byte[] result=(byte[]) msg.obj; callBack.LoadContetn(result); if(msg.what==1) { dialog.dismiss(); } } }; // @SuppressWarnings("unused") class MyThread implements Runnable{ @Override public void run() { // TODO Auto-generated method stub HttpClient httpClient=new DefaultHttpClient(); HttpPost httpPost=new HttpPost(path); try { HttpResponse response=httpClient.execute(httpPost); if(response.getStatusLine().getStatusCode()==200) { byte[] result=EntityUtils.toByteArray(response.getEntity()); Message message=new Message(); message.obj=result; message.what=1; handler.sendMessage(message); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ httpClient.getConnectionManager().shutdown(); } } } //启动线程 new Thread(new MyThread()).start(); dialog.show(); } public interface DownLoadCallBack{ public void LoadContetn(byte[] result); } }
MainActivity.java
package com.example.android_handler_callback;
import com.example.android_handler_callback.DownLoadContent.DownLoadCallBack;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private final String img_path="http://www.baidu.com/img/bdlogo.gif";
private Button button;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button1);
imageView=(ImageView)this.findViewById(R.id.imageView1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
DownLoadContent downLoadContent=new DownLoadContent(MainActivity.this);
downLoadContent.downLoad(img_path, new DownLoadCallBack() {
@Override
public void LoadContetn(byte[] result) {
// TODO Auto-generated method stub
Bitmap bitmap=BitmapFactory.decodeByteArray(result, 0, result.length);
imageView.setImageBitmap(bitmap);
}
});
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
AndroidManifest.xml 在清单文件中添加网络权限
相关文章推荐
- Android开发之使用Handler封装下载图片工具类(源码分享)
- android学习——handler的学习及实现封装下载图片功能
- android图片下载工具类
- android里图片下载工具类AsyncImageLoader分析
- [置顶] Android开发之java代码工具类。判断当前网络是否连接并请求下载图片
- android里图片下载工具类AsyncImageLoader分析
- Android 图片三级缓存机制工具类封装
- <android里图片下载工具类AsyncImageLoader分析> 后续:优化
- 网络请求以及网络请求下载图片的工具类 android开发java工具类
- Android 图片加载工具类 Universal-Image-Loader 的封装和使用示例
- Android 图片下载工具类:BitmapUtil
- <android里图片下载工具类AsyncImageLoader分析> 后续:优化
- 基于Piccaso的图片下载工具类封装
- 网络请求以及网络请求下载图片的工具类 android开发java工具类
- Android 图片三级缓存机制工具类封装
- android里图片下载工具类AsyncImageLoader分析
- Android 图片下载工具类:BitmapUtil
- Android 各种工具类 图片下载工具类:BitmapUtil
- Android 图片三级缓存机制工具类封装
- 毕加索的艺术——Picasso,一个强大的Android图片下载缓存库,OkHttpUtils的使用,二次封装PicassoUtils实现微信精选