欢迎加入QQ交流3群:317874559

package com.android.xiong.urltest;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.MalformedURLException;import java.net.URL;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.Menu;import android.widget.ImageView;public class MainActivity extends Activity {ImageView show;Bitmap bitmap;Handler handler = new Handler() {@Overridepublic void handleMessage(Message msg) {if (msg.what == 0x123) {// 使用ImageView显示该图片show.setImageBitmap(bitmap);}}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);show = (ImageView) findViewById(R.id.show);new Thread() {@Overridepublic void run() {// 定义一个URL对象URL url;try {url = new URL("http://img1.gtimg.com/news/pics/hv1/37/195/1468/95506462.jpg");// 打开该URL的资源输入流InputStream is = url.openStream();// 从InputStream中解析出图片bitmap = BitmapFactory.decodeStream(is);// 发送消息handler.sendEmptyMessage(0x123);is.close();// 再次打开RL对应的资源输入流is = url.openStream();// 打开手机文件对应的输出流OutputStream os = openFileOutput("KEQIANG.JPG", MODE_APPEND);byte[] buff = new byte[1024];int hasRead = 0;// 将URL资源下载到本地while ((hasRead = is.read(buff)) > 0) {os.write(buff, 0, hasRead);}is.close();os.close();} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}.start();}@Overridepublic 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;}}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity" >    <ImageView         android:id="@+id/show"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:contentDescription="@string/hello_world"/></LinearLayout>


</pre><pre code_snippet_id="86820" snippet_file_name="blog_20131128_4_1113442" name="code" class="html"> <uses-permission         android:name="android.permission.INTERNET"/>



更多相关文章

  1. Android在线资源API
  2. EditText插入表情图片
  3. android操作sqlite3的blob字段,显示数据中的图片
  4. android 获取SD卡的图片及其路径
  5. Android:读取本地相册与相机获取图片上传到服务器(用字符串的形式
  6. Android 图片缩放与旋转的实现详解
  7. 图片的放大缩小
  8. android获取assert资源文件
  9. 关于android 调用系统图片浏览器并返回图片路径问题

随机推荐

  1. android 可滑动的View
  2. 解决 ButterKnife8.2.1 与 Dagger2.5 冲
  3. webview 底部被遮住
  4. Android(安卓)Studio基础之项目目录结构(
  5. Android(安卓)手机震动
  6. 动态设置 view 宽高
  7. 提供一些Android免费课程分享给大家
  8. Android(安卓)OpenGL例子学习
  9. 在AndroidManifest.xml文件中的android:w
  10. android获取短信所有内容