android图片下载器
页面布局
<span style="white-space:pre"> </span><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="图片下载器" android:gravity="center"android:id="@+id/title"android:textSize="30sp"/><EditTextandroid: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"/><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/button"android:layout_below="@id/url"android:text="链接"android:onClick="onClick"/><ImageViewandroid: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;@Overrideprotected 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"/>
链接一张百度的图片