我该如何去code JPEG图像连接codeD以Base64在android系统,看看它在一个ImageView的? [英] How do I decode a jpeg image encoded in Base64 in android and see it on an ImageView?

查看:151
本文介绍了我该如何去code JPEG图像连接codeD以Base64在android系统,看看它在一个ImageView的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器发送一个带codeD Base64的字符串作为我的Andr​​oid设备。在那之后,我去code中的Base64编码字符串中此方法以它的绘制。我看不到图像,当我在一个Itemizedoverlay添加它。

 公众可绘制操作SetIcon(字符串输入){    字节[] B = Base64.de code(输入,Base64.DEFAULT);
    位图德codedByte = BitmapFactory.de codeByteArray的(B,0,b.length个);
    可绘制可绘制=新BitmapDrawable(德codedByte);
    drawable.setBounds(0,0,50,50);
    返回绘制;
}公共无效setphotopoint(字符串输入){
    可绘制可绘制=操作SetIcon(输入);
    PhotoOverlay的aPhotoOverlay =新PhotoOverlay的(绘制,这一点);
    OverlayItem overlayitem =新OverlayItem();
    overlayitem.setMarker(绘制);
    aPhotoOverlay.addOverlay(overlayitem);
    overlays.add(aPhotoOverlay);
}

这是我的PhotoOverlay的类

 公共类PhotoOverlay的扩展ItemizedOverlay< OverlayItem> {    私人的ArrayList< OverlayItem>项目=新的ArrayList< OverlayItem>();    上下文mContext;    公共PhotoOverlay的(可绘制defaultMarker,上下文的背景下){
        超(boundCenterBottom(defaultMarker));
        this.mContext =背景;
    }
    公共PhotoOverlay的(可绘制defaultMarker){
        超(boundCenterBottom(defaultMarker));
        // TODO自动生成构造函数存根
    }    保护布尔中的onTap(INT指数){
        返回true;
    }    公共无效addOverlay(OverlayItem叠加){
        items.add(覆盖);
        // setLastFocusedIndex(-1);
        填充();
    }
    @覆盖
    保护OverlayItem createItem中(int i)以{
        // TODO自动生成方法存根
        返回items.get(ⅰ);
    }
    公共无效清除(){
        items.clear();
        填充();
    }    公共无效removeOverlay(OverlayItem叠加){
        items.remove(覆盖);
        填充();
    }    @覆盖
    公众诠释大小(){
        // TODO自动生成方法存根
        返回items.size();
    }
}


解决方案

将您的二进制文件为Base64然后使用以下code找回它:

 公共静态无效base64ToFile(字符串路径,字符串strBase64)
            抛出IOException
        字节[]字节= Base64.de code(strBase64);
        byteArrayTofile(路径,字节);
    }公共静态无效byteArrayTofile(字符串路径,字节[]字节)
        抛出IOException
    文件镜像文件=新的文件(路径);
    文件DIR =新的文件(imagefile.getParent());
    如果(!dir.exists()){
        dir.mkdirs();
    }
    FOS的FileOutputStream =新的FileOutputStream(镜像文件);
    fos.write(字节);
    fos.close();
}


二进制文件转换为Base64:

 公共静态字符串fileToBase64(字符串路径)抛出IOException
    字节[]字节= fileToByteArray(路径);
    返回Base64.en codeBytes(字节);
}公共静态的byte [] fileToByteArray(字符串路径)抛出IOException
    文件镜像文件=新的文件(路径);
    字节[]数据=新的字节[(INT)imagefile.length()];
    FIS的FileInputStream =新的FileInputStream(镜像文件);
    fis.read(数据);
    fis.close();
    返回的数据;
}

My server sends a encoded Base64 string to my android device. After that, I decode the Base64 string in this method to make a drawable of it. I can't see the image when I add it in an Itemizedoverlay.

public Drawable seticon(String input){

    byte[] b = Base64.decode(input, Base64.DEFAULT);
    Bitmap decodedByte = BitmapFactory.decodeByteArray(b, 0, b.length);
    Drawable drawable = new BitmapDrawable(decodedByte);
    drawable.setBounds(0, 0, 50, 50);
    return drawable;
}

public void setphotopoint(String input){
    Drawable drawable = seticon(input);
    PhotoOverlay aPhotoOverlay = new PhotoOverlay(drawable, this);
    OverlayItem overlayitem = new OverlayItem();
    overlayitem.setMarker(drawable);
    aPhotoOverlay.addOverlay(overlayitem);
    overlays.add(aPhotoOverlay);
}

this is my PhotoOverlay class

public class PhotoOverlay extends ItemizedOverlay<OverlayItem>{

    private ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();

    Context mContext ;

    public PhotoOverlay(Drawable defaultMarker, Context context) {
        super(boundCenterBottom(defaultMarker));
        this.mContext = context;
    }
    public PhotoOverlay(Drawable defaultMarker) {
        super(boundCenterBottom(defaultMarker));
        // TODO Auto-generated constructor stub
    }

    protected boolean onTap(int index) {
        return true;
    }

    public void addOverlay(OverlayItem overlay) {
        items.add(overlay);
        //setLastFocusedIndex(-1);
        populate();
    }
    @Override
    protected OverlayItem createItem(int i) {
        // TODO Auto-generated method stub
        return items.get(i);
    }
    public void clear() {
        items.clear();
        populate();
    }

    public void removeOverlay(OverlayItem overlay) {
        items.remove(overlay);
        populate();
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return items.size();
    }
}

解决方案

Convert your binary file to Base64 then use the following code to retrieve it:

public static void base64ToFile(String path, String strBase64)
            throws IOException {
        byte[] bytes = Base64.decode(strBase64);
        byteArrayTofile(path, bytes);
    }

public static void byteArrayTofile(String path, byte[] bytes)
        throws IOException {
    File imagefile = new File(path);
    File dir = new File(imagefile.getParent());
    if (!dir.exists()) {
        dir.mkdirs();
    }
    FileOutputStream fos = new FileOutputStream(imagefile);
    fos.write(bytes);
    fos.close();
}


converting binary file to Base64:

public static String fileToBase64(String path) throws IOException {
    byte[] bytes = fileToByteArray(path);
    return Base64.encodeBytes(bytes);
}

public static byte[] fileToByteArray(String path) throws IOException {
    File imagefile = new File(path);
    byte[] data = new byte[(int) imagefile.length()];
    FileInputStream fis = new FileInputStream(imagefile);
    fis.read(data);
    fis.close();
    return data;
}

这篇关于我该如何去code JPEG图像连接codeD以Base64在android系统,看看它在一个ImageView的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆