图片转为String传个给服务端

本文介绍如何在Android中将图片转换为String并通过POST请求发送到服务端。该方法适用于处理头像等长字符串传输场景。代码示例中包含了从String还原图片的步骤,并提到了所需Java类。
摘要由CSDN通过智能技术生成

上传头像等图片比较简单的一种就是直接把图片--- >String 再作为参数post给服务端

之所以用post是因为string都会很长~


下面见代码

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 - 〉图片

                                byte[] tempb = Base64Coder.decode(tp);
				Bitmap bitmap = BitmapFactory.decodeByteArray(tempb, 0, tempb.length);
				ImageVIew.setImageBitmap(bitmap);


另外这个方法用到两个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
	 *         
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值