【问题标题】:How do I decode a jpeg image encoded in Base64 in android and see it on an ImageView?如何在android中解码以Base64编码的jpeg图像并在ImageView上查看它?
【发布时间】:2012-04-15 06:12:24
【问题描述】:

我的服务器向我的安卓设备发送一个编码的 Base64 字符串。之后,我在此方法中解码 Base64 字符串以使其可绘制。当我将图像添加到 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);
}

这是我的 PhotoOverlay 课程

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();
    }
}

【问题讨论】:

    标签: android bytearray base64 jpeg itemizedoverlay


    【解决方案1】:

    将您的二进制文件转换为 Base64,然后使用以下代码检索它:

    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();
    }
    

    将二进制文件转换为 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;
    }
    

    【讨论】:

    • 将二进制转换为Base64,这是否意味着我可以将jpeg转换为二进制>> Base64而不转换为位图?如果是这样,那就太棒了!
    猜你喜欢
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    相关资源
    最近更新 更多