android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte

简介:

http://blog.csdn.net/z104207/article/details/6634774


[java]  view plain copy
  1. package com.bingo.util;  
  2.   
  3. import java.io.BufferedOutputStream;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.io.InputStream;  
  9.   
  10. import android.graphics.Bitmap;  
  11. import android.graphics.BitmapFactory;  
  12. import android.graphics.Matrix;  
  13.   
  14. public class ImageDispose {  
  15.       
  16.       
  17.       
  18.     /** 
  19.      * @param 将图片内容解析成字节数组 
  20.      * @param inStream 
  21.      * @return byte[] 
  22.      * @throws Exception 
  23.      */  
  24.     public static byte[] readStream(InputStream inStream) throws Exception {  
  25.         byte[] buffer = new byte[1024];  
  26.         int len = -1;  
  27.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  28.         while ((len = inStream.read(buffer)) != -1) {  
  29.             outStream.write(buffer, 0, len);  
  30.         }  
  31.         byte[] data = outStream.toByteArray();  
  32.         outStream.close();  
  33.         inStream.close();  
  34.         return data;  
  35.   
  36.     }  
  37.     /** 
  38.      * @param 将字节数组转换为ImageView可调用的Bitmap对象 
  39.      * @param bytes 
  40.      * @param opts 
  41.      * @return Bitmap 
  42.      */  
  43.     public static Bitmap getPicFromBytes(byte[] bytes,  
  44.             BitmapFactory.Options opts) {  
  45.         if (bytes != null)  
  46.             if (opts != null)  
  47.                 return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,  
  48.                         opts);  
  49.             else  
  50.                 return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);  
  51.         return null;  
  52.     }  
  53.     /** 
  54.      * @param 图片缩放 
  55.      * @param bitmap 对象 
  56.      * @param w 要缩放的宽度 
  57.      * @param h 要缩放的高度 
  58.      * @return newBmp 新 Bitmap对象 
  59.      */  
  60.     public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h){  
  61.         int width = bitmap.getWidth();  
  62.         int height = bitmap.getHeight();  
  63.         Matrix matrix = new Matrix();  
  64.         float scaleWidth = ((float) w / width);  
  65.         float scaleHeight = ((float) h / height);  
  66.         matrix.postScale(scaleWidth, scaleHeight);  
  67.         Bitmap newBmp = Bitmap.createBitmap(bitmap, 00, width, height,  
  68.                 matrix, true);  
  69.         return newBmp;  
  70.     }  
  71.       
  72.     /** 
  73.      * 把Bitmap转Byte 
  74.      * @Author HEH 
  75.      * @EditTime 2010-07-19 上午11:45:56 
  76.      */  
  77.     public static byte[] Bitmap2Bytes(Bitmap bm){  
  78.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  79.         bm.compress(Bitmap.CompressFormat.PNG, 100, baos);  
  80.         return baos.toByteArray();  
  81.     }  
  82.     /** 
  83.      * 把字节数组保存为一个文件 
  84.      * @Author HEH 
  85.      * @EditTime 2010-07-19 上午11:45:56 
  86.      */  
  87.     public static File getFileFromBytes(byte[] b, String outputFile) {  
  88.         BufferedOutputStream stream = null;  
  89.         File file = null;  
  90.         try {  
  91.             file = new File(outputFile);  
  92.             FileOutputStream fstream = new FileOutputStream(file);  
  93.             stream = new BufferedOutputStream(fstream);  
  94.             stream.write(b);  
  95.         } catch (Exception e) {  
  96.             e.printStackTrace();  
  97.         } finally {  
  98.             if (stream != null) {  
  99.                 try {  
  100.                     stream.close();  
  101.                 } catch (IOException e1) {  
  102.                     e1.printStackTrace();  
  103.                 }  
  104.             }  
  105.         }  
  106.         return file;  
  107.     }  
  108.           
  109. }  


相关文章
|
5月前
|
Android开发 开发者
Android自定义View之不得不知道的文件attrs.xml(自定义属性)
本文详细介绍了如何通过自定义 `attrs.xml` 文件实现 Android 自定义 View 的属性配置。以一个包含 TextView 和 ImageView 的 DemoView 为例,讲解了如何使用自定义属性动态改变文字内容和控制图片显示隐藏。同时,通过设置布尔值和点击事件,实现了图片状态的切换功能。代码中展示了如何在构造函数中解析自定义属性,并通过方法 `setSetting0n` 和 `setbackeguang` 实现功能逻辑的优化与封装。此示例帮助开发者更好地理解自定义 View 的开发流程与 attrs.xml 的实际应用。
Android自定义View之不得不知道的文件attrs.xml(自定义属性)
|
5月前
|
Java Android开发
Android studio中build.gradle文件简单介绍
本文解析了Android项目中build.gradle文件的作用,包括jcenter仓库配置、模块类型定义、包名设置及依赖管理,涵盖本地、库和远程依赖的区别。
457 19
|
5月前
|
存储 XML Java
Android 文件数据储存之内部储存 + 外部储存
简介:本文详细介绍了Android内部存储与外部存储的使用方法及核心原理。内部存储位于手机内存中,默认私有,适合存储SharedPreferences、SQLite数据库等重要数据,应用卸载后数据会被清除。外部存储包括公共文件和私有文件,支持SD卡或内部不可移除存储,需申请权限访问。文章通过代码示例展示了如何保存、读取、追加、删除文件以及将图片保存到系统相册的操作,帮助开发者理解存储机制并实现相关功能。
1149 2
|
8月前
|
移动开发 安全 Java
Android历史版本与APK文件结构
通过以上内容,您可以全面了解Android的历史版本及其主要特性,同时掌握APK文件的结构和各部分的作用。这些知识对于理解Android应用的开发和发布过程非常重要,也有助于在实际开发中进行高效的应用管理和优化。希望这些内容对您的学习和工作有所帮助。
738 83
|
12月前
|
ARouter Android开发
Android不同module布局文件重名被覆盖
Android不同module布局文件重名被覆盖
|
14天前
|
开发工具 Android开发
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
189 11
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
|
24天前
|
Java 开发工具 Maven
【01】完整的安卓二次商业实战-详细的初级步骤同步项目和gradle配置以及开发思路-优雅草伊凡
【01】完整的安卓二次商业实战-详细的初级步骤同步项目和gradle配置以及开发思路-优雅草伊凡
93 6

热门文章

最新文章

推荐镜像

更多
  • DNS