本节采用两种方法创建Bitmap对象。主要过程是在xml中创建两个组件ImageView和Button。点击按钮下载图片,并做显示。创建了工具类HttpUtils,下载使用。
activity_main.xml
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:orientation="vertical" > <Button android:id="@+id/button_" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/downImg" /> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/hello_world"/> </LinearLayout>
HttpUtils.java
package com.example.imageviewhttp; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class HttpUtils { private final static String URL_PATH = "http://192.168.78.1:8081/web/pic/logo_2.jpg";//访问网络图片的路径 public HttpUtils() { } /** * 从网络中获取图片信息,以流的形式返回 * @return */ public static InputStream getImageViewInputStream(){ InputStream inputStream = null; try { URL url = new URL(URL_PATH); if(url!=null) { HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setConnectTimeout(3000); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setDoInput(true) ; int resonpseCode = httpURLConnection.getResponseCode(); if(resonpseCode == 200){ inputStream = httpURLConnection.getInputStream(); } } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return inputStream; } /** * 从网络中获取图片西悉尼,以字节数组的形式放回 * @return */ public static byte[] getImageViewArray(){ byte [] data = null; InputStream inputStream = null; // 不需要关闭的输出流,直接写入内存中。 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { URL url = new URL(URL_PATH); if(url!=null) { HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setConnectTimeout(3000); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setDoInput(true) ; int resonpseCode = httpURLConnection.getResponseCode(); int len = 0; byte[] b_data = new byte[1024]; if(resonpseCode == 200){ inputStream = httpURLConnection.getInputStream(); ; while ((len =inputStream.read(b_data)) !=-1){ outputStream.write(b_data, 0, len); } data = outputStream.toByteArray(); } } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(inputStream!=null){try { inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }} } return data; } }
MainActivity.java
import java.io.InputStream; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity implements OnClickListener{ private ImageView imageview; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageview = (ImageView) this.findViewById(R.id.imageView); button = (Button) this.findViewById(R.id.button_); button.setOnClickListener(this); } @Override public 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; } public void onClick(View v) { // 方法一 // InputStream inputStream = HttpUtils.getImageViewInputStream(); // Bitmap bitmap = BitmapFactory.decodeStream(inputStream); // 方法二 byte[] data = HttpUtils.getImageViewArray(); Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); imageview.setImageBitmap(bitmap); } }
相关推荐
通过图片的url获取到图片,并显示到imageview中,本实例中选择的图片的百度官网的logo。仅提供一种获取网络图片的方法。
android ImageView网络图片加载、动态设置尺寸、圆角..
可以下载网络图片并存储到本地, 实现了基本的简单的图片下载并显示的功能
width=100%就可以使得图片宽度充满屏幕,高度自适应
android 将Imageview 将view生成图片后保存到本地相册的实现生成图片后保存到本地相册的实现,android 将Imageview生成图片后保存到本地相册的实现 android 将Imageview生成图片后保存到本地相册的实现, 有需要的朋友...
android根据url下载图片的网络ImageView
在imageview上的指定位置添加另一个iamgeview,并实现等比例缩放,图片自适应
android imageview 图片缩放 图片缩放、移动+异步加载+Activity半透明
Android ImageView实现是对图片放大后可以局部的拉伸放大,双击放大
从网络加载图片显示到listview上,主函数布局是一个ListView item布局是TextView和ImageView 数量的多少视情况而定 再写一个网络请求数据的类 最后面有代码: ”urlConnection 请求数据类的封装“ 类名一定要一样...
android imageView 读取网络图片并进行
自定义ImageView类,能够设置为网络图片的格式
主要介绍了Android中ImageView使用网络图片资源的方法,较为详细的分析了ImageView调用网络图片的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
android自定义ImageView实现旋转动画
CaptchaImageView:自定义ImageView生成验证码图片
一个带有缓存的下载网络图片的ImageView,两层缓存,内存缓存以及文件缓存
android 圆角 自定义 ImageView 支持网络下载,模拟器显示会有问题,真机没问题
这是我找了半天的demo,自定义的ImageView从网络获取图片在显示到手机上缩放,再改成适合自己的缩放图片,公布给需要找图片缩放功能的小伙伴。
android 获取网络图片 通过HttpURLConnection获取网络图片bitmap 并将图片显示到imageView中
Android中ImageView实现平铺多张图片Android中实现平铺图片有两种方式:在drawable中定义平铺的Bitmap然后在ImageView中引