Android开发之使用Handler封装下载图片工具类
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Android开发之使用Handler封装下载图片工具类,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3718字,纯文字阅读大概需要6分钟。
内容图文
如果每下载一张图片,就得重写一次Http协议,多线程的启动和handler的信息传递就显得太麻烦了,我们直接来封装一个工具类,便于我们以后在开发时随时可以调用。
(1)在清单文件添加权限
<uses-permission android:name="android.permission.INTERNET"/>
(2)编辑工具类
package com.example.g05_handler; import java.io.IOException; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import android.annotation.SuppressLint; import android.app.ProgressDialog; import android.content.Context; import android.os.Handler; import android.os.Message; import android.util.Log; public class DownLoad { private ProgressDialog dialog; public DownLoad(Context context) { // TODO Auto-generated constructor stub dialog = new ProgressDialog(context); dialog.setTitle("提示"); dialog.setMessage("玩命加载中"); } @SuppressLint("HandlerLeak") public void Down(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.download(result); if (msg.what == 1) { dialog.dismiss(); } } }; class MyThread implements Runnable { @Override public void run() { // TODO Auto-generated method stub HttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(path); try { HttpResponse httpResponse = client.execute(httpGet); Log.i("TAG", "------>" + httpResponse.getStatusLine().getStatusCode()); if (httpResponse.getStatusLine().getStatusCode() == 200) { byte[] result = EntityUtils.toByteArray(httpResponse .getEntity()); Message message = Message.obtain(); message.obj = result; message.what = 1; handler.sendMessage(message); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (client != null) { client.getConnectionManager().shutdown(); } } }; } new Thread(new MyThread()).start(); dialog.show(); } public interface DownLoadCallback { public void download(byte[] data); } }
(3)调用该工具类
package com.example.g05_handler; import com.example.g05_handler.DownLoad.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 Button button; private ImageView imageView; private final String path="http://avatar.csdn.net/D/7/5/1_u013900875.jpg"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button=(Button)this.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 DownLoad downLoad=new DownLoad(MainActivity.this); downLoad.Down(path, new DownLoadCallback() { @Override public void download(byte[] data) { // TODO Auto-generated method stub Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.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; } }
原文:http://blog.csdn.net/gerogelin/article/details/24908457
内容总结
以上是互联网集市为您收集整理的Android开发之使用Handler封装下载图片工具类全部内容,希望文章能够帮你解决Android开发之使用Handler封装下载图片工具类所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。