AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="shortcut.song.com.myapplication.UrlTestActivity">    <ImageView        android:id="@+id/url_show"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />LinearLayout>
package shortcut.song.com.myapplication;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.ImageView;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.MalformedURLException;import java.net.URL;public class UrlTestActivity extends AppCompatActivity {    ImageView imageView;    Bitmap bitmap;   Handler handler = new Handler(){       @Override       public void handleMessage(Message msg) {           super.handleMessage(msg);           if (msg.what == 0x1234) {               imageView.setImageBitmap(bitmap);           }       }   };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_url_test);        imageView = (ImageView)findViewById(R.id.url_show);        new Thread(){            @Override            public void run() {                super.run();                try {                    // 定义一个URL对象                    URL url = new URL("http://192.168.8.27/files/JPG/9.jpg");                    // 打开该URL对应的资源输入流                    InputStream is = url.openStream();                    // 从InputStream中解析出图片                    bitmap = BitmapFactory.decodeStream(is);                    // 发送消息,通知UI组件加载图片                    handler.sendEmptyMessage(0x1234);                    is.close();                    // 再次打开该URL对应的资源输入流                    is = url.openStream();                    // 打开文件对应的输出流                    OutputStream os = openFileOutput("9.jpg", MODE_PRIVATE);                    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) {                    e.printStackTrace();                } catch (IOException e) {                    e.printStackTrace();                }            }        }.start();    }}

运行效果(通过URL加载的图片):

更多相关文章

  1. android 处理图片工具
  2. android submenu
  3. Android(安卓)双击和手势的图片缩放
  4. android HttpClient 上传图片
  5. android背景模糊化处理
  6. Android(安卓)WebView的加载超时处理
  7. android中颜色对应的值
  8. Android(安卓)– 加载图片本缓存到内存与本地
  9. android点击实现图片放大缩小

随机推荐

  1. android 一键新机,改机概述 xposed 改机缺
  2. android异步线程利用Handler将消息发送至
  3. Android 手机跳转到权限管理界面汇总
  4. 总结】Android辅助功能(一)-Accessibilit
  5. 挣得第一桶金
  6. Android中关于dip和px以及转换的总结(重
  7. Android布局(相对布局和网格布局)
  8. [android盈利模式探索]我也分享一下我And
  9. Android学习系列--App调试的几个命令实践
  10. ArrayList 扩容 Android Java 真的不一样