Android开发之异步任务加载网络图片并存储在sdcard中(源代码分享)
在MainActivity中的代码
package com.example.f02_sdcard02; import android.os.AsyncTask; import android.os.Bundle; import android.app.Activity; import android.app.ProgressDialog; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Log; 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 ProgressDialog dialog; //创建一个对话框 private String path = "http://111.0.26.119:8080/http/hangzhou.jpg"; //图片所在的url private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dialog = new ProgressDialog(MainActivity.this); dialog.setTitle("提示"); dialog.setMessage("load......"); button = (Button) this.findViewById(R.id.button1); imageView=(ImageView)this.findViewById(R.id.imageView1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub new MyTask().execute(path);//启动异步任务 } }); } public class MyTask extends AsyncTask<String, Void, byte[]> { private String imageName; @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); dialog.show(); } @Override protected byte[] doInBackground(String... arg0) { // TODO Auto-generated method stub byte []data=HttpUtiles.downloadImg(arg0[0]); //获取图片名称 imageName=arg0[0].substring(arg0[0].lastIndexOf("/")+1,arg0[0].length() ); Log.i("TAG", "-------->"+data.length); return data; } @Override protected void onPostExecute(byte[] result) { // TODO Auto-generated method stub super.onPostExecute(result); Bitmap bitmap=BitmapFactory.decodeByteArray(result, 0, result.length); //显示图片 imageView.setImageBitmap(bitmap); FileUtiles fileUtiles=new FileUtiles(); fileUtiles.fileSave(imageName, result); dialog.dismiss(); } } @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; } }
HttpUtile获取网络数据的方法
package com.example.f02_sdcard02; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; 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.util.Log; public class HttpUtiles { public static byte[] downloadImg(String path) { byte[] data = null; HttpClient client=new DefaultHttpClient(); HttpPost httpPost=new HttpPost(path); try { HttpResponse httpResponse=client.execute(httpPost); if(httpResponse.getStatusLine().getStatusCode()==200){ data=EntityUtils.toByteArray(httpResponse.getEntity()); Log.i("TAG", "-------->"+data.length); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ client.getConnectionManager().shutdown(); } return data; }
存储在sdcard中的代码在上一篇博客中已贴出,在这篇博客中就不在写了,Android通过AsyncTask加载网络数据在实际开发中有着很大的作用,因耗时长的网络操作放在主线程中会堵塞UI,就要采取多线程的方式解决该问题。最后一定不要忘了在 清单文件中添加 <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。