如何解析android中的json base64图像

时间:2014-12-03 18:20:55

标签: android json imageview

嗨,我有这个代码用于解析其他json并且工作正常:

try{
        JSONArray json = jparser.GetJSONfromUrl(url);
        for(int i =0;i <json.length();i++){
            try {
                JSONObject c = json.getJSONObject(i);
                String vpeli = c.getString(TAG_PELI);
                String vname2 = c.getString(TAG_nombre2);
            } catch (JSONException e) {
                // TODO: handle exception
                e.printStackTrace();
                return "error creando variables";
            }

我想将json base64图像解析为android imageview,我的json是:

<?php  

$con = mysql_connect('mysql.hostinger.es', 'u453215752_aitor', '*******');   
mysql_query("SET CHARACTER SET utf8");  
mysql_query("SET NAMES utf8");  

$cities['cities'] = array();

if( $con )  
{  
mysql_select_db('u453215752_droid');  

$res = mysql_query('select name, nametwo, photo from cities');

while( $row = mysql_fetch_array($res) ) { 
array_push($cities['cities'], array('name' => $row['name'], 'nametwo' => 
$row['nametwo'], 'photo' => base64_encode($row['photo'])));
}
mysql_free_result($res);
mysql_close($con);
}
header('Content-type: application/json');
echo json_encode($cities);

我在imageview中解析“照片”有什么用? 谢谢!

1 个答案:

答案 0 :(得分:0)

我希望我的答案完整,你只想从你已经拥有的JSON字符串解码一个Bitmap?

试试这个

private Bitmap decodeBitmap(String input) 
    {
        byte[] byteArr = Base64.decode(input, 0);
        return BitmapFactory.decodeByteArray(byteArr, 0, byteArr.length); 
    }