android byte[]与图片的转换
生活随笔
收集整理的這篇文章主要介紹了
android byte[]与图片的转换
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
今天,簡單講講android如何將byte數組的數據轉換成圖片顯示。
之前,在做一個功能時,從服務器獲得了圖片的byte數組的數據,需要將數據轉成圖片顯示在手機上,或者保存在文件里。當時居然不知道怎么轉換,所以在網上查找了資料,最終是解決了問題。這里記錄一下。
直接上代碼:
package com.bingo.util;import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream;import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix;public class ImageDispose {/*** @param 將圖片內容解析成字節數組* @param inStream* @return byte[]* @throws Exception*/public static byte[] readStream(InputStream inStream) throws Exception {byte[] buffer = new byte[1024];int len = -1;ByteArrayOutputStream outStream = new ByteArrayOutputStream();while ((len = inStream.read(buffer)) != -1) {outStream.write(buffer, 0, len);}byte[] data = outStream.toByteArray();outStream.close();inStream.close();return data;}/*** @param 將字節數組轉換為ImageView可調用的Bitmap對象* @param bytes* @param opts* @return Bitmap*/public static Bitmap getPicFromBytes(byte[] bytes,BitmapFactory.Options opts) {if (bytes != null)if (opts != null)return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,opts);elsereturn BitmapFactory.decodeByteArray(bytes, 0, bytes.length);return null;}/*** @param 圖片縮放* @param bitmap 對象* @param w 要縮放的寬度* @param h 要縮放的高度* @return newBmp 新 Bitmap對象*/public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h){int width = bitmap.getWidth();int height = bitmap.getHeight();Matrix matrix = new Matrix();float scaleWidth = ((float) w / width);float scaleHeight = ((float) h / height);matrix.postScale(scaleWidth, scaleHeight);Bitmap newBmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,matrix, true);return newBmp;}/*** 把Bitmap轉Byte* @Author HEH* @EditTime 2010-07-19 上午11:45:56*/public static byte[] Bitmap2Bytes(Bitmap bm){ByteArrayOutputStream baos = new ByteArrayOutputStream();bm.compress(Bitmap.CompressFormat.PNG, 100, baos);return baos.toByteArray();}/*** 把字節數組保存為一個文件* @Author HEH* @EditTime 2010-07-19 上午11:45:56*/public static File getFileFromBytes(byte[] b, String outputFile) {BufferedOutputStream stream = null;File file = null;try {file = new File(outputFile);FileOutputStream fstream = new FileOutputStream(file);stream = new BufferedOutputStream(fstream);stream.write(b);} catch (Exception e) {e.printStackTrace();} finally {if (stream != null) {try {stream.close();} catch (IOException e1) {e1.printStackTrace();}}}return file;}}簡單講講,這里面其實已經有byte[]和bitmap的轉換,轉換成bitmap后,就可以直接顯示到界面上。但是如何需要將圖片的byte[]存儲進入手機的文件里,那應該怎么辦呢?其實也很簡單。
private void bytesToImageFile(byte[] bytes) {try {File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/aaa.jpeg");FileOutputStream fos = new FileOutputStream(file);fos.write(bytes, 0, bytes.length);fos.flush();fos.close();} catch (Exception e) {e.printStackTrace();}簡單講講,其實就是定義一個后綴名為jpg的文件名,然后使用輸出流將byte[]寫入文件就可以了。
android byte[]與圖片的轉換就講完了。
就這么簡單。
總結
以上是生活随笔為你收集整理的android byte[]与图片的转换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android setGravity()
- 下一篇: android 调用系统播放器