Как декодировать изображение в формате jpeg, закодированное в Base64, в Android и увидеть его в ImageView?

Мой сервер отправляет закодированную строку Base64 на мое устройство Android. После этого я декодирую строку 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();
    }
}

person user1334164    schedule 15.04.2012    source источник


Ответы (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;
}
person Bobs    schedule 15.04.2012
comment
Преобразование двоичного файла в Base64 означает ли это, что я могу преобразовать jpeg в двоичный ›› Base64 без преобразования в растровое изображение? Если это так, то это гениально! - person Siddharth Menon; 12.02.2013