android Java BASE64编码和解码二:图片的编码和解码
生活随笔
收集整理的這篇文章主要介紹了
android Java BASE64编码和解码二:图片的编码和解码
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1、準(zhǔn)備工作
?(1)在項(xiàng)目中集成 Base64 代碼,集成方法見(jiàn)第一篇博文:android Java BASE64編碼和解碼一:基礎(chǔ)???
?(2)添加 ImgHelper?工具類
?
package com.app21; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.IOException;import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Bitmap.CompressFormat; import android.util.Base64; import sun.misc.BASE64Decoder.encoder.BASE64Decoder; import sun.misc.BASE64Decoder.encoder.BASE64Encoder;public class ImgHelper {/*** TODO:將byte數(shù)組以Base64方式編碼為字符串* @param bytes 待編碼的byte數(shù)組* @return 編碼后的字符串* */public static String encode(byte[] bytes){return new BASE64Encoder().encode(bytes);}/*** TODO:將以Base64方式編碼的字符串解碼為byte數(shù)組* @param encodeStr 待解碼的字符串* @return 解碼后的byte數(shù)組* @throws IOException * */public static byte[] decode(String encodeStr) throws IOException{byte[] bt = null; BASE64Decoder decoder = new BASE64Decoder(); bt = decoder.decodeBuffer(encodeStr);return bt;}/*** TODO:將兩個(gè)byte數(shù)組連接起來(lái)后,返回連接后的Byte數(shù)組* @param front 拼接后在前面的數(shù)組* @param after 拼接后在后面的數(shù)組* @return 拼接后的數(shù)組* */public static byte[] connectBytes(byte[] front, byte[] after){byte[] result = new byte[front.length + after.length];System.arraycopy(front, 0, result, 0, after.length);System.arraycopy(after, 0, result, front.length, after.length);return result;}/*** TODO:將圖片以Base64方式編碼為字符串* @param imgUrl 圖片的絕對(duì)路徑(例如:D:\\jsontest\\abc.jpg)* @return 編碼后的字符串* @throws IOException * */public static String encodeImage(String imgUrl) throws IOException{FileInputStream fis = new FileInputStream(imgUrl);byte[] rs = new byte[fis.available()];fis.read(rs);fis.close();return encode(rs);}/*** 將Bitmap轉(zhuǎn)換成字符串* @param bitmap* @return*/public static String bitmaptoString(Bitmap bitmap) {String string = null;ByteArrayOutputStream bStream = new ByteArrayOutputStream();bitmap.compress(CompressFormat.PNG, 100, bStream);byte[] bytes = bStream.toByteArray();string = Base64.encodeToString(bytes, Base64.DEFAULT);return string;}/*** 把byte數(shù)組轉(zhuǎn)化成 bitmap對(duì)象* @param b* @return*/public static Bitmap bytes2Bimap(byte[] b) {if (b.length != 0) {return BitmapFactory.decodeByteArray(b, 0, b.length);} else {return null;}} }?
2、把drawable里面的 圖片進(jìn)行編碼和解碼
??????主要布局
?????
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/container"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.app21.MainActivity"tools:ignore="MergeRootFrame" ><Buttonandroid:id="@+id/bt"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="點(diǎn)擊到Sd卡文件界面內(nèi)" /><ImageViewandroid:id="@+id/image1"android:layout_width="100dp"android:layout_height="100dp" /></LinearLayout>?
? 主要代碼:
??
package com.app21; import java.io.IOException; import java.io.InputStream;import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView;/*** @author admin* 對(duì)drawable里面的圖片進(jìn)行存取*/ public class MainActivity extends Activity {ImageView imageView1 ;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main );imageView1 = (ImageView) findViewById( R.id.image1 ) ;//得到bitmap流字符串String bitmapString = ImgHelper.bitmaptoString( getBitmap() ) ;try {Bitmap bitmap = ImgHelper.bytes2Bimap( ImgHelper.decode( bitmapString )) ;imageView1.setImageBitmap( bitmap ) ;} catch (IOException e) {e.printStackTrace();}Button button = (Button) findViewById( R.id.bt ) ;button.setOnClickListener( new OnClickListener() {@Overridepublic void onClick(View v) {startActivity( new Intent( MainActivity.this , MainActivityFile.class ));}});}//得到bitmappublic Bitmap getBitmap(){InputStream inputStream = getResources().openRawResource(R.drawable.ic_launcher ); BitmapDrawable drawable = new BitmapDrawable(inputStream); Bitmap bitmap = drawable.getBitmap(); return bitmap ;} }
3、對(duì)Sd卡中的圖片進(jìn)行編碼和解碼
??? 主要布局
???
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 tools:ignore="MergeRootFrame" > 7 8 <ImageView 9 android:id="@+id/image_file" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" /> 12 13 </LinearLayout>?主要代碼
??
package com.app21; import java.io.File; import java.io.FileOutputStream; import java.io.IOException;import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.widget.ImageView;public class MainActivityFile extends Activity {ImageView imageView1 ;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_file );imageView1 = (ImageView) findViewById( R.id.image_file ) ;String str ;//將圖片轉(zhuǎn)化為字符串try {str = ImgHelper.encodeImage( getFileName() );Bitmap bitmap = ImgHelper.bytes2Bimap( ImgHelper.decode( str )) ;imageView1.setImageBitmap( bitmap ) ;} catch (IOException e) {e.printStackTrace();}}/*** 把圖片存到本地* @return sd卡圖片的路徑*/String getFileName(){Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.libingbing );File SpicyDirectory = new File("/sdcard/Images/");SpicyDirectory.mkdirs(); String filename="/sdcard/Images/" + "test11111" + ".jpg";FileOutputStream out = null ;try {out = new FileOutputStream(filename);bmp.compress(Bitmap.CompressFormat.JPEG , 100 , out);}catch (Exception e) {e.printStackTrace();}finally{try {out.flush();}catch (IOException e){e.printStackTrace();}}try {out.close();} catch (IOException e){e.printStackTrace();}out=null;return filename ;} }?
4、注意事項(xiàng) :
???? 在對(duì)SD卡中的圖片編碼和解碼是需要添加權(quán)限
??????
<!-- 在SDCard中創(chuàng)建與刪除文件權(quán)限 -->??? <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
??? <!-- 往SDCard寫入數(shù)據(jù)權(quán)限 -->
??? <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
??? <!-- 從SDCard讀取數(shù)據(jù)權(quán)限 -->
??? <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
5、運(yùn)行結(jié)果 :
?
???? ???????
?
6、項(xiàng)目下載地址:
????? http://download.csdn.net/detail/yanzi2015/8712419
?
?7、其他圖片Base64編碼的相關(guān)博客
???? http://www.cnblogs.com/coco1s/p/4375774.html
??
轉(zhuǎn)載于:https://www.cnblogs.com/zhaoyanjun/p/4511939.html
與50位技術(shù)專家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的android Java BASE64编码和解码二:图片的编码和解码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 关于intent传递数据的练习
- 下一篇: 一键u启动怎么制作u盘 如何制作一键U启