Android实现将Base64Encoder编码转为图片学习笔记(二)
关于
关于将图片转换为base64编码的方法,请参考文章Android使用 BASE64Encoder实现图片的Base64编码转换(可用来存储到数据库中)学习笔记(一)本文直接在上一篇基础上修改。
效果
实现第一步,修改activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/colorPrimaryDark">
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/image_base64"
android:layout_width="150dp"
android:layout_height="150dp"
/>
<EditText
android:id="@+id/edit_base664"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="@+id/btn_photo"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="25dp"
android:text="拍照"
android:textSize="16sp" />
<Button
android:id="@+id/btn_jiema"
android:text="解码"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/ima_jiema"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
第二步,修改CharacterEncoder.java
package com.example.a13115.blog;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.nio.ByteBuffer;
public abstract class CharacterEncoder {
protected PrintStream pStream;
protected abstract int bytesPerAtom();
protected abstract int bytesPerLine();
protected void encodeBufferPrefix(OutputStream paramOutputStream)
throws IOException {
this.pStream = new PrintStream(paramOutputStream);
}
protected void encodeBufferSuffix(OutputStream paramOutputStream)
throws IOException {
}
protected void encodeLinePrefix(OutputStream paramOutputStream, int paramInt)
throws IOException {
}
protected void encodeLineSuffix(OutputStream paramOutputStream)
throws IOException {
this.pStream.println();
}
protected abstract void encodeAtom(OutputStream paramOutputStream, byte[] paramArrayOfByte, int paramInt1, int paramInt2)
throws IOException;
protected int readFully(InputStream paramInputStream, byte[] paramArrayOfByte)
throws IOException {
for (int i = 0; i < paramArrayOfByte.length; i++) {
int j = paramInputStream.read();
if (j == -1) {
return i;
}
paramArrayOfByte[i] = ((byte) j);
}
return paramArrayOfByte.length;
}
public void encode(InputStream paramInputStream, OutputStream paramOutputStream)
throws IOException {
byte[] arrayOfByte = new byte[b