<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/urlpath" />    <EditText        android:id="@+id/urlpath"        android:text="http://ww1.sinaimg.cn/mw690/6532527agw1ej5sd7j0jgj20eq09u3z5.jpg"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:inputType="" />    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/button" />    <ImageView        android:id="@+id/show"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>


package com.example.utils;import java.io.ByteArrayOutputStream;import java.io.InputStream;public class StreamTool {public static byte[] readInputStream(InputStream inStream) throws Exception {ByteArrayOutputStream outStream = new ByteArrayOutputStream();// 输出流,写到内存byte[] buffer = new byte[1024];// 1K缓冲区容量int len = 0;while ((len = inStream.read(buffer)) != -1) {outStream.write(buffer, 0, len);// 从数组buffer中读取从0到len的数据}inStream.close();return outStream.toByteArray();}}


package com.example.image;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import com.example.utils.StreamTool;public class ImageService {public static byte[] getImage(String path) throws Throwable {URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("GET");conn.setConnectTimeout(5 * 1000);InputStream inStream = conn.getInputStream();// 通过输入流获取图片数据return StreamTool.readInputStream(inStream);// 得到的图片的二进制数据}}

package com.example.image;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.ImageView;import android.widget.Toast;public class ImageActivity extends Activity {private static final String TAG = "ImageActivity";private EditText pathText;private ImageView imageView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_image);pathText = (EditText) this.findViewById(R.id.urlpath);imageView = (ImageView) this.findViewById(R.id.show);Button button = (Button) this.findViewById(R.id.button);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {String path = pathText.getText().toString();try {byte[] data = ImageService.getImage(path);Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,data.length);imageView.setImageBitmap(bitmap);// 显示图片} catch (Throwable e) {Toast.makeText(ImageActivity.this, R.string.error, Toast.LENGTH_LONG).show();Log.e(TAG, e.toString());}}});}}// <!-- 访问internet权限 -->// <uses-permission android:name="android.permission.INTERNET"/>



更多相关文章

  1. Android(安卓)-- 图片画画板(canvas、paint、bitmap)
  2. android中Json数据保存方式
  3. android 打开本地相册,并将选择的照片显示到当前界面
  4. android 关闭数据连接方法
  5. Android腾讯微薄客户端开发十二:博主的偶像
  6. Android(安卓)HTTP GET/POST
  7. 转:Android之通信录中的联系人操作
  8. Android(安卓)自定义Drawable 实现圆角矩形和圆形图片
  9. mybatisplus的坑 insert标签insert into select无参数问题的解决

随机推荐

  1. 每天学一个 Linux 命令(27):head
  2. 每天学一个 Linux 命令(28):ln
  3. 每天学一个 Linux 命令(29):tail
  4. 排序算法——冒泡排序
  5. 2021年Clion激活码来了,亲测永久有效!
  6. 后台八 文章分类
  7. Java Lambda 表达式
  8. 深入理解Java Stream流水线
  9. ELK 之 elasticsearch集群安装
  10. 如何记忆 Spring Bean 的生命周期