android新手求问 byte[]转base64改怎么做?

ladyishenlong 2017-12-20 09:29:13
String s = new String(Base64.encodeBase64(photo));
这是我之前用的方法,里面的photo是一张图片转成的byte[]但是得到的结果是Qk3OlwAAAAAAADYAAAAoAAAAZgAAAH……这样的,但是我现在条用一个api需要的格式是/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEB......这样的,请问应该怎么弄才行?
...全文
650 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
试试这个 转base64
ByteArrayOutputStream baos = new ByteArrayOutputStream();
		String base64 = null;
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(baos);
			oos.writeObject(Bitmap);
			base64 = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if (oos != null) {
				try {
					oos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
转Bitmap
byte[] objectBytes = Base64.decode(str.getBytes(), Base64.DEFAULT);
		ByteArrayInputStream bais = new ByteArrayInputStream(objectBytes);
		
		ObjectInputStream ois = null;
		PoiBase poiBase = null;
		try {
			ois = new ObjectInputStream(bais);
			Bitmap = (Bitmap) ois.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if (ois != null) {
				try {
					ois.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
Jing丶無雙 2017-12-20
  • 打赏
  • 举报
回复
result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
//改成这个试试
result = new String(Base64.encode(bitmapBytes,Base64.NO_WRAP));
ladyishenlong 2017-12-20
  • 打赏
  • 举报
回复
引用 1 楼 xj396282771 的回复:
试试下面这个方法,把你的图片转为bitmap对象即可
/** 
 * bitmap转为base64 
 * @param bitmap 
 * @return 
 */  
public static String bitmapToBase64(Bitmap bitmap) {  
  
    String result = null;  
    ByteArrayOutputStream baos = null;  
    try {  
        if (bitmap != null) {  
            baos = new ByteArrayOutputStream();  
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);  
  
            baos.flush();  
            baos.close();  
  
            byte[] bitmapBytes = baos.toByteArray();  
            result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);  
        }  
    } catch (IOException e) {  
        e.printStackTrace();  
    } finally {  
        try {  
            if (baos != null) {  
                baos.flush();  
                baos.close();  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
    return result;  
}
这个我刚刚试了,可是得到的base64再转回来图片是缺失的
assky124 2017-12-20
  • 打赏
  • 举报
回复
android.util.Base64.encodeToString(buffer, Base64.NO_WRAP); 试试,后面的参数会影响生成的BASE64,多试几次就行了
Jing丶無雙 2017-12-20
  • 打赏
  • 举报
回复
试试下面这个方法,把你的图片转为bitmap对象即可
/** 
 * bitmap转为base64 
 * @param bitmap 
 * @return 
 */  
public static String bitmapToBase64(Bitmap bitmap) {  
  
    String result = null;  
    ByteArrayOutputStream baos = null;  
    try {  
        if (bitmap != null) {  
            baos = new ByteArrayOutputStream();  
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);  
  
            baos.flush();  
            baos.close();  
  
            byte[] bitmapBytes = baos.toByteArray();  
            result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);  
        }  
    } catch (IOException e) {  
        e.printStackTrace();  
    } finally {  
        try {  
            if (baos != null) {  
                baos.flush();  
                baos.close();  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
    return result;  
}

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧