android图片下载器
来源:互联网 发布:淘宝客服月薪一般多少 编辑:程序博客网 时间:2024/06/05 07:30
android图片下载器
页面布局
<span style="white-space:pre"></span><TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="图片下载器" android:gravity="center" android:id="@+id/title" android:textSize="30sp"/><EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/title" android:text="https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png" android:id="@+id/url"/><Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button" android:layout_below="@id/url" android:text="链接" android:onClick="onClick"/><ImageView android:layout_below="@id/button" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/picture" android:src="@drawable/ic_launcher" android:scaleType="center"/>
页面布局使用相对布局RelativeLayout,有四个组件:
TextView:标题
EditText:输入url
Button:执行图片获取
ImageView:展示图片
图示
主页面Java代码
public Button btn;public EditText et;public ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.button); et = (EditText) findViewById(R.id.url); iv = (ImageView) findViewById(R.id.picture); }
onCreate()方法获取XML组件的id
onClick(View v)方法监听按钮事件
public void onClick(View v){ if(v.getId() == R.id.button){ final String path = et.getText().toString().trim(); if(path == null || "".equals(path)){//检查数据合法性 Toast.makeText(getApplicationContext(), "图片地址不能为空", Toast.LENGTH_SHORT).show(); return ; } Toast.makeText(getApplicationContext(), "图片加载中", Toast.LENGTH_SHORT).show(); //http为耗时操作,不可以在UI线程(主线程)中运行程序 new Thread(new Runnable(){public void run() {try{final Bitmap bitmap = ImageUtil.getImageURLGet(path);runOnUiThread(new Runnable(){public void run() {iv.setImageBitmap(bitmap);}});}catch(Exception e){e.printStackTrace();runOnUiThread(new Runnable(){public void run() {Toast.makeText(getApplicationContext(), "图片加载失败", Toast.LENGTH_SHORT).show();};});}} }).start(); } }
按钮事件内部检验是否为链接按钮,检查数据合法性,url是否为空,接着获取互联网上的图片,然后用runOnUiThread()方法去把获取到的图片呈现出来,主线程是UI线程,负责UI处理,不支持耗时操作,所以要创建一个线程去访问www,下载好了图片后,呈现出来,如果出现异常,报“图片加载失败”错误。
访问互联网
public class ImageUtil {public static Bitmap getImageURLGet(String path)throws Exception{URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("GET");//GET POSTconn.setConnectTimeout(5000);//设置超时时间int status = conn.getResponseCode();if(status == 200){InputStream inputStream = conn.getInputStream();byte[] bytes = StreamUtil.getBytesByInputStream(inputStream);Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);return bitmap;}throw new RuntimeException("超時");}}
链接一个URL路径,获取链接,设置超时时间,获取相应码,200代表成功,获取输入流,接收图片数据信息,创建一个Bitmap,返回。
Java获取图片数据信息
<span style="white-space:pre"></span>public static byte[] getBytesByInputStream(InputStream in){ByteArrayOutputStream bos = new ByteArrayOutputStream();try{byte[] bytes = new byte[2048];int len = 0;while( (len = in.read(bytes))!= -1 ){bos.write(bytes,0,len);}}catch(Exception e){e.printStackTrace();}return bos.toByteArray();}
最后别忘了加上网络访问权限:
<uses-permission android:name="android.permission.INTERNET"/>
链接一张百度的图片
1 0
- android图片下载器
- android图片下载工具类
- android 图片下载并显示
- android 图片下载 类
- android 图片下载、放大缩小
- android图片下载并保存
- Android实现图片下载
- Android图片下载机制
- Android中的Fresco图片下载
- Android 图片下载并显示
- 多线程批量图片下载器
- 图片下载器类
- 百度图片下载器
- 图片下载
- 图片下载
- 图片下载
- 图片下载
- 图片下载
- LeetCode 67. Add Binary
- Search a 2D Matrix II
- 使用jquery静态资源公共库cdn及回退地址
- 【中位数的应用】邮局设置
- Oracle 找出需要回收高水位的表
- android图片下载器
- Centos6.X下安装Eclipse
- tomcat get 中文乱码
- [刷题]算法竞赛入门经典 3-10/UVa1587 3-11/UVa1588
- Java Calendar 类的时间操作
- Ubuntu 手机开发培训准备
- 可重入函数和线程安全的区别与联系
- 转:Maven常用命令
- Cpp环境【Usaco2.1.3】【Vijos1222】顺序排分数