布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView              android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="网络图片的地址是:\n 这个可以自定在输入框里面输入,但是为了简便,直接使用TextView做了" />    <Button        android:id="@+id/Button01"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:text="得到网络图片" />        <ImageView        android:id="@+id/imageView01"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        /></LinearLayout>


MainActivity:

public class MainActivity extends Activity {private Button Button01;private ImageView imageView01;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button01 = (Button) findViewById(R.id.Button01);imageView01 = (ImageView) findViewById(R.id.imageView01);Button01.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {String path = "http://111.114.116.236:8080/Test/image/Test.jpg";try {byte[] data = ImageService.getImageByByte(path);Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,data.length);imageView01.setImageBitmap(bitmap);} catch (Exception e) {e.printStackTrace();}}});}}


ImageService:

public class ImageService {public static byte[] getImageByByte(String path) throws Exception {ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();URL url = new URL(path);// HttpClientConnection 是对HttpURLConnection的封装,不建议使用/*HttpsURLConnection与HttpURLConnection就是http与https的区别 *  */HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();httpURLConnection.setConnectTimeout(5000);httpURLConnection.setRequestMethod("GET");if (httpURLConnection.getResponseCode() == 200) {InputStream inputStream = httpURLConnection.getInputStream();byte[] buffer = new byte[1024];int len = 0;// 读到buffer中间while ((len = inputStream.read(buffer)) != -1) {// 从buffer中读数据byteArrayOutputStream.write(buffer, 0, len);}inputStream.close();}return byteArrayOutputStream.toByteArray();}}


更多相关文章

  1. Android中json操作数据的使用
  2. Android(安卓)Hook学习之ptrace函数的使用
  3. 【Android】共享APK中的资源文件
  4. android使用javamail 发送邮件遇到的问题
  5. Android(安卓)studio图片ERROR: 9-patch image xx .9.png malfor
  6. android 网络获取图片并存储
  7. 箭头函数的基础使用
  8. NPM 和webpack 的基础使用
  9. Python list sort方法的具体使用

随机推荐

  1. Anaconda 安装 国内镜像问题解决方案
  2. 怎样设计绘制人物?动漫人物设计方法
  3. 怎样学画漫画?画漫画步骤!
  4. Python可变与不可变数据、深拷贝与浅拷贝
  5. 开发方式的变化:docker-compose 迁移到 mi
  6. LFS系列镜像在阿里云镜像站首发上线
  7. Vue中的计算属性与监听属性
  8. vue事件绑定、列表渲染、计算属性与侦听
  9. 提高EDA(探索性数据分析)效率的 3 个简单工
  10. Python 中的 requirements.txt 与 setup.