android开发--获取网络图片,读取进度条
来源:互联网 发布:李连杰国际影响力 知乎 编辑:程序博客网 时间:2024/06/05 03:21
1、读取进度条的效果
2、成功读取到图片的效果
1.首先是Activity
package cn.itcast.pic;import android.app.Activity;import android.app.ProgressDialog;import android.graphics.Bitmap;import android.os.Bundle;import android.os.Handler;import android.view.View;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;public class mainActivity extends Activity { private TextView addressET; private ImageView imageIV; private Handler handler=new Handler(); //在主线程中创建handler private ProgressDialog dialog; private ImageService service; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //1、获取对话框的id addressET = (TextView) this.findViewById(R.id.addressET); imageIV = (ImageView) this.findViewById(R.id.imageIV); service = new ImageService(this); } public void onClick(View v){ //把图片放在一个新的线程里面来读取. new Thread(){//创建一个新的线程 public void run(){ try { String address = addressET.getText().toString(); handler.post(new Runnable() {//此处用一个匿名内部类,runnable自动把消息发送给主线程创建处理的handler,主线程会自动更新。 public void run() { dialog=new ProgressDialog(mainActivity.this); dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);//设置进度条样式 dialog.setMessage("请稍候..."); dialog.setCancelable(false);//判断是否取消进度条 dialog.show(); } }); //由于网络操作比较耗时,所以在新线程中操作 final Bitmap image = service.getImage(address); handler.post(new Runnable() { public void run() { dialog.dismiss(); imageIV.setImageBitmap(image);//新线程更新界面,需要使用handler } }); } catch (Exception e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), "服务器忙,请稍后再试!", 0).show(); } } }.start(); }}
2.然后是读取图片的service
package cn.itcast.pic;import java.io.File;import java.io.FileOutputStream;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLEncoder;import android.accounts.NetworkErrorException;import android.content.Context;import android.graphics.Bitmap;import android.graphics.Bitmap.CompressFormat;import android.graphics.BitmapFactory;public class ImageService { private Context context; public ImageService(Context context) { super(); this.context = context; } public Bitmap getImage(String address) throws Exception { // 1.通过Url对象封装url对象 URL url = new URL(address); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(3000); // 2.判断是否有缓存文件 File cacheFile = new File(context.getCacheDir(), URLEncoder.encode(address));// 缓存文件 if (cacheFile.exists())// 判断是否有缓存 conn.setIfModifiedSince(cacheFile.lastModified());// 发送缓存文件的最后修改时间 // 3.获取状态码,根据状态吗来判断接下来的操作。读取文件?还是写缓存,写缓存的时候记着用多个线程 int code = conn.getResponseCode(); if (code == 200) { byte[] data = Util.read(conn.getInputStream()); Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);// 转化为图片 weiteCache(cacheFile, bm); return bm; } else if(code==304){ return BitmapFactory.decodeFile(cacheFile.getAbsolutePath());//读取本地数据,并转为图片来显示 } // 如果不成功,抛异常,给我们自己看的 throw new NetworkErrorException("访问服务器出错:"+code); } // 4. 写缓存文件 private void weiteCache(final File cacheFile, final Bitmap bm) { // 使用一个新的线程来写,这样的好处就是在页面打开的时候不会因为写缓存而等待时间 new Thread() { public void run() { try { FileOutputStream fos = new FileOutputStream(cacheFile); bm.compress(CompressFormat.JPEG, 100, fos);//指定格式 存储到本地 fos.close(); } catch (Exception e) { throw new RuntimeException(e); } } }.start(); }}
3.读取网络文件的一个工具类
package cn.itcast.pic;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;public class Util { public static byte[] read(InputStream in) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) != -1) baos.write(buffer, 0, len); baos.close(); byte[] data = baos.toByteArray(); return data; } }
4.主界面XML
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <EditText android:id="@+id/addressET" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:inputType="textEmailAddress" > <requestFocus /> </EditText> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text=" GO " /> </LinearLayout> <ImageView android:id="@+id/imageIV" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
0 0
- android开发--获取网络图片,读取进度条
- android开发--获取网络图片,读取进度条
- android开发——获取网络图片
- Android开发之网络图片获取
- android读取网络图片
- android读取网络图片
- Android读取网络图片
- Android手机开发:从网络上读取图片
- Android手机开发:从网络上读取图片
- 【Android学习笔记系列】AsyncTask的使用和介绍(获取网络图片与进度条实例)
- android - 获取网络图片
- android获取网络图片
- android获取网络图片
- Android获取网络图片
- android 获取网络图片
- android 获取网络图片
- android 获取网络图片
- Android获取网络图片
- [225]Implement Stack using Queues
- 回溯法总结
- NGS library construction(未完待续)
- Arduino与Processing串口通信问题
- MFC 托盘实现
- android开发--获取网络图片,读取进度条
- SpriteBuilder中CCB精灵对象的Sprite frame为什么有时候不能修改
- [225]Implement Stack using Queues
- 【Python】Learn Python the hard way, ex7 字符串连接
- sql中的分析函数
- 数据结构之顺序表的操作
- Mantle JSONkeyPath辅助插件
- C语言输入输出函数详解
- 诚实人和说谎者 -- truth teller and liar