Android之简单了解Bitmap显示图片及缓存图片

博观而约取,厚积而薄发。这篇文章主要讲述Android之简单了解Bitmap显示图片及缓存图片相关的知识,希望能为你提供帮助。
  昨天我们学了如何连接网络,今天我们就学习一下如何从把网上图片显示到项目中
今天主要用到的是Bitmap 类
Bitmap是android系统中的图像处理的最重要类之一。用它可以获取图像文件信息,进行图像剪切、旋转、缩放等操作,并可以指定格式保存图像文件
具体作用属性参考官方API: https://msdn.microsoft.com/zh-cn/library/system.drawing.bitmap(v=vs.110).aspx
不多说,看案例吧
  做一个图片显示器:
activity_main.xml

1 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2xmlns:tools="http://schemas.android.com/tools" 3android:layout_width="match_parent" 4android:layout_height="match_parent" 5android:orientation="vertical" 6tools:context="com.example.imagelook.MainActivity" > 7 8< EditText 9android:id="@+id/et_path" 10android:layout_width="match_parent" 11android:layout_height="wrap_content" 12android:hint="@string/editText" > 13< /EditText> 14 15< Button 16android:id="@+id/button1" 17android:layout_width="wrap_content" 18android:layout_height="wrap_content" 19android:onClick="click" 20android:text="@string/btn" /> 21 22< ImageView 23android:id="@+id/iv" 24android:layout_width="wrap_content" 25android:layout_height="wrap_content"/> 26 27 < /LinearLayout>

mainAcitivity.java
package com.example.imagelook; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; 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.util.Base64; import android.view.View; import android.widget.EditText; import android.widget.ImageView; public class MainActivity extends Activity {private EditText et_path; private ImageView iv; private Handler handler = new Handler(){ //处理消息 public void handleMessage(Message msg) {Bitmap bitmap = (Bitmap) msg.obj; iv.setImageBitmap(bitmap); }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //1、找控件 et_path = (EditText) findViewById(R.id.et_path); iv = (ImageView) findViewById(R.id.iv); }//2、点击按钮进行查看指定路径的源码 public void click(View v) throws IOException {new Thread(){ public void run(){ try { //2.1、获取访问图片的路径 String path = et_path.getText().toString().trim(); File file = new File(getCacheDir(),Base64.encodeToString(path.getBytes(), Base64.DEFAULT)); //test.png if( file.exists() & & file.length()> 0 ) { //使用缓存的图片 System.out.println("使用缓存图片"); Bitmap cacheBitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); //把cacheBitmap显示到iv上 Message msg = Message.obtain(); msg.obj = cacheBitmap ; handler.sendMessage(msg); }else{ //第一次访问 联网缓存图片 System.out.println("第一次访问连接网络"); //2.2 创建URL对象 URL url = new URL(path); //2.3 获取httpURLConnection HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //2.4 设置请求方式 conn.setRequestMethod("GET"); //2.5设置超时时间 conn.setConnectTimeout(5000); //2.6获取服务器状态码 int code = conn.getResponseCode(); if (code == 200){ //2.7 获取图片数据 不管是什么数据(text 图片)都是以流行式返回 InputStream in = conn.getInputStream(); //2.7缓存图片 谷歌提供一个缓存目录 FileOutputStream fos =new FileOutputStream(file); int len = -1 ; byte[] buffer = new byte[1024]; //1kb while((len = in.read(buffer)) != -1){ fos.write(buffer,0,len); } fos.close(); in.close(); //2.8 通过位图工厂获取bitmap //Bitmap bitmap = BitmapFactory.decodeStream(in); Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); //读取缓存//2.9 把bitmap 显示到iv上 Message msg = Message.obtain(); //消息池有消息池拿数据,没有new一个 msg.obj = bitmap ; handler.sendMessage(msg); //发消息}} } catch (Exception e) { e.printStackTrace(); } }; }.start(); }}

Android之简单了解Bitmap显示图片及缓存图片

文章图片

【Android之简单了解Bitmap显示图片及缓存图片】配置好就可以运行看下效果了
 
Android之简单了解Bitmap显示图片及缓存图片

文章图片

 
下面说一下,案例的小细节吧!
因为图片如果每次加载,每次都要从网上读取数据流,显示到手机上面,这样很容易浪费客户流量,所以第一访问时可以下载图片,以后访问直接访问内存里的就好了。
1、判断客户是否是第一次访问
2、多线程访问网络
Android之简单了解Bitmap显示图片及缓存图片

文章图片

Android之简单了解Bitmap显示图片及缓存图片

文章图片

 
第一次访问,创建cache文件夹并保存文件
 
Android之简单了解Bitmap显示图片及缓存图片

文章图片

打印日志
 
Android之简单了解Bitmap显示图片及缓存图片

文章图片

  再次访问,从文件中取出
Android之简单了解Bitmap显示图片及缓存图片

文章图片

  我们可以在手机中清除缓存
例如海马玩模拟器:设置—> 应用—> 照片查看器—> 清除缓存
Android之简单了解Bitmap显示图片及缓存图片

文章图片

 
如果我们不给图片起名字,像这样 
Android之简单了解Bitmap显示图片及缓存图片

文章图片

下面是生成的文件名
 
Android之简单了解Bitmap显示图片及缓存图片

文章图片

  所以我们手机经常会莫名其妙的出现一些我们看不懂得文件,
其实只是做了这一个小小的动作,让用户不敢随便删除我们的文件
Android之简单了解Bitmap显示图片及缓存图片

文章图片

 

    推荐阅读