如何在Android上将Base64字符串转换为Bitmap?

时间:2015-02-27 14:51:08

标签: android json base64 decode

我在Android应用程序中将多个Base64字符串转换为Bitmap图像时遇到此问题。 现在我已经找了很长一段时间的解决方案,但似乎没有什么能解决我的问题。 我的应用程序从服务器获取JSON数据,包含base64中的多个图像。从服务器收到的json代码可以在这里找到:http://pastebin.com/tgiMgqRg 我试图解码这个网站上的第一张图片:http://www.opinionatedgeek.com/dotnet/tools/base64decode/,它似乎是正确的。

接收JSON日期的代码是:

    public JSONObject makeHttpRequest(String url, String method,
        List<NameValuePair> params) {

    // Making HTTP request
    try {
        HttpClient httpClient = sslClient(new DefaultHttpClient());

        // check for request method
        if(method == "POST"){
            // request method is POST
            //DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        }else if(method == "GET"){
            // request method is GET
            //DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);
            //DEBUG
            Log.d("JSONParser", "sent request : " + url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        }           

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}

然后我尝试使用以下代码解码这些图像:

    try {
       images = json.getJSONArray(MapActivity.TAG_LIST);

            // looping through all Images
            for (int i = 0; i < images.length(); i++) {
                JSONObject house = images.getJSONObject(i);
                String photo = house.getString(TAG_PHOTO);
                //Log.d("PHOTO", photo);
                Log.d("PHOTO NUMBER", i + "");

                try {
                    byte[] decodedString = Base64.decode(photo, Base64.URL_SAFE);
                    Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
                    HashMap<String, Bitmap> map = new HashMap<String, Bitmap>();
                    map.put(TAG_PHOTO, decodedByte);

                    imagesList.add(map);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

但是我得到了这些错误:

    D/PHOTO NUMBER(26854): 0
D/skia(26854): --- SkImageDecoder::Factory returned null
D/PHOTO NUMBER(26854): 1
D/skia(26854): --- SkImageDecoder::Factory returned null
D/PHOTO NUMBER(26854): 2
D/skia(26854): --- SkImageDecoder::Factory returned null
D/PHOTO NUMBER(26854): 3
W/System.err(26854): java.lang.IllegalArgumentException: bad base-64
W/System.err(26854):    at android.util.Base64.decode(Base64.java:161)
W/System.err(26854):    at android.util.Base64.decode(Base64.java:136)
W/System.err(26854):    at android.util.Base64.decode(Base64.java:118)
02W/System.err(26854):  at ...$LoadImages.doInBackground(DisplayHouseInfo.java:225)
W/System.err(26854):    at ...$LoadImages.doInBackground(DisplayHouseInfo.java:1)
W/System.err(26854):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
W/System.err(26854):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err(26854):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
W/System.err(26854):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err(26854):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err(26854):    at java.lang.Thread.run(Thread.java:841)
D/PHOTO NUMBER(26854): 4
W/System.err(26854): java.lang.IllegalArgumentException: bad base-64
W/System.err(26854):    at android.util.Base64.decode(Base64.java:161)
                                            ...
D/PHOTO NUMBER(26854): 5
W/System.err(26854): java.lang.IllegalArgumentException: bad base-64
W/System.err(26854):    at android.util.Base64.decode(Base64.java:161)
                                            ...
D/PHOTO NUMBER(26854): 6
W/System.err(26854): java.lang.IllegalArgumentException: bad base-64
W/System.err(26854):    at android.util.Base64.decode(Base64.java:161)
                                            ...
02-27 14:57:11.316: E/BitmapFactory(26854): Unable to decode stream: java.io.FileNotFoundException: /: open failed: EISDIR (Is a directory)
I/System.out(26854): resolveUri failed on bad bitmap uri: 
E/BitmapFactory(26854): Unable to decode stream: java.io.FileNotFoundException: /: open failed: EISDIR (Is a directory)
I/System.out(26854): resolveUri failed on bad bitmap uri: 

我的问题是: - 我似乎无法使BitmapFactory.decodeByteArray工作 - 我为持久图像获得了另一个例外

您对如何解决这个问题有什么想法/建议吗?

1 个答案:

答案 0 :(得分:0)

表示图像的前三个base64字符串解码正常。但是以下四个在线上产生bad base-64的捕获

byte[] decodedString = Base64.decode(photo, Base64.URL_SAFE);
之前我说过。如果你愿意的话

byte[] decodedString = Base64.decode(photo, Base64.DEFAULT);

然后没有捕获。所有7个base64字符串都可以解码。