当前位置:编程学习 > JAVA >>

图片转为String传个给服务端

上传头像等图片比较简单的一种就是直接把图片--- >String 再作为参数post给服务端
之所以用post是因为string都会很长~
 
下面见代码
[java]  
try {  
                ByteArrayOutputStream stream = new ByteArrayOutputStream();  
                 photo.compress(Bitmap.CompressFormat.JPEG, 60, stream);  
                 byte[] b = stream.toByteArray();  
                 // 将图片流以字符串形式存储下来  
                 String tp = new String(Base64Coder.encodeLines(b));//tp 就是最终的参数  
  
            } catch (Exception e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
 
 
为了检验本地可以尝试把string - 〉图片
[java]  
                            byte[] tempb = Base64Coder.decode(tp);  
Bitmap bitmap = BitmapFactory.decodeByteArray(tempb, 0, tempb.length);  
ImageVIew.setImageBitmap(bitmap);  
 
另外这个方法用到两个java类在下面
[java]  
import java.io.IOException;  
  
import android.graphics.Bitmap;  
import android.graphics.BitmapFactory;  
  
public class Base64 {  
    /** prevents anyone from instantiating this class */  
    private Base64() {  
    }  
  
    /** 
     * This character array provides the alphabet map from RFC1521. 
     */  
    private final static char ALPHABET[] = {  
            // 0 1 2 3 4 5 6 7  
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 0  
            'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 1  
            'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 2  
            'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', // 3  
            'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 4  
            'o', 'p', 'q', 'r', 's', 't', 'u', 'v', // 5  
            'w', 'x', 'y', 'z', '0', '1', '2', '3', // 6  
            '4', '5', '6', '7', '8', '9', '+', '/' // 7  
    };  
  
    /** 
     * Decodes a 7 bit Base64 character into its binary value. 
     */  
    private static int valueDecoding[] = new int[128];  
  
    /** 
     * initializes the value decoding array from the character map 
     */  
    static {  
        for (int i = 0; i < valueDecoding.length; i++) {  
            valueDecoding[i] = -1;  
        }  
  
        for (int i = 0; i < ALPHABET.length; i++) {  
            valueDecoding[ALPHABET[i]] = i;  
        }  
    }  
  
    /** 
     * Converts a byte array into a Base64 encoded string. 
     *  
     * @param data 
     *            bytes to encode 
     * @param offset 
     *            which byte to start at 
     * @param length 
     *            how many bytes to encode; padding will be added if needed 
     * @return base64 encoding of data; 4 chars for every 3 bytes 
     */  
    public static String encode(byte[] data, int offset, int length) {  
        int i;  
        int encodedLen;  
        char[] encoded;  
  
        // 4 chars for 3 bytes, run input up to a multiple of 3  
        encodedLen = (length + 2) / 3 * 4;  
        encoded = new char[encodedLen];  
  
        for (i = 0, encodedLen = 0; encodedLen < encoded.length; i += 3, encodedLen += 4) {  
            encodeQuantum(data, offset + i, length - i, encoded, encodedLen);  
        }  
  
        return new String(encoded);  
    }  
  
    /** 
     * Encodes 1, 2, or 3 bytes of data as 4 Base64 chars. 
     *  
     * @param in 
     *            buffer of bytes to encode 
     * @param inOffset 
     *            where the first byte to encode is 
     * @param len 
     *            how many bytes to encode 
     * @param out 
     *            buffer to put the output in 
     * @param outOffset 
     *            where in the output buffer to put the chars 
     */  
    private static void encodeQuantum(byte in[], int inOffset, int len,  
            char out[], int outOffset) {  
补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,