2012-01-27 121 views
1

我在发送图像base64编码字符串时出现问题。我有总是相同的错误“不支持的标记类型0xb7”,在服务器和响应总是“java.io.FileNotFoundException”使用base64编码图像并发送到服务器

/** 
* This method convert a image in a string base64 encoded 
* 
* @return String Base64 
*/ 
public final static String getImageBase64(String fileName){ 
    String encodedImage = null; 

    Bitmap bm = BitmapFactory.decodeFile(Constants.ROUTE_IMAGES + fileName);  
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    if (bm != null){//Encuentra la imagen 
     bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
     byte[] b = baos.toByteArray(); 
     try{ 
      encodedImage = Base64.encodeToString(b, Base64.DEFAULT); 
     }catch(Exception ex){ 
      Log.d(TAG, "Error codificacion en Base64"); 
     } 
    }else{//NO encuentra la imagen 
     Log.d(TAG, "bm is NULL"); 
    } 

的下一个方法是将数据发送到服务器

/** 
* Method to send information + image encoded base64 to a server and receive a response 
* 
* @param params String with information + image encoded base64 
* @param urlToConnect URL server 
* @throws Exception 
*/ 
public final static String sendData(String params, String urlToConnect) throws Exception{ 
    URL url = null; 
    HttpURLConnection conn = null; 

    try{ 
     url = new URL(urlToConnect); 
     conn = (HttpURLConnection)url.openConnection(); 
     conn.setRequestMethod("POST"); 
     conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
     conn.setRequestProperty("Content-Length", "" + Integer.toString(params.getBytes().length)); 
     conn.setRequestProperty("Content-Language", "en-US"); 

     conn.setUseCaches (false); 
     conn.setDoInput(true); 
     conn.setDoOutput(true); 

     DataOutputStream wr = new DataOutputStream (conn.getOutputStream()); 
     wr.writeBytes (params); 
     wr.flush(); 
     wr.close(); 
     if (conn.getResponseCode() == HttpURLConnection.HTTP_OK){ 
      Log.d(TAG, "Conexión OK"); 
     }else{ 
      Log.d(TAG, "Conexión FALLIDA"); 
     } 
     //Get Response -- Resultado 
     InputStream is = conn.getInputStream(); 
     BufferedReader rd = new BufferedReader(new InputStreamReader(is), 8*Constants.K); 
     String line; 
     StringBuffer response = new StringBuffer(); 
     if (rd.readLine() == null){ 
      Log.d(TAG, "Resultado NULL"); 
     } 
     while((line = rd.readLine()) != null) { 
      response.append(line); 
      response.append('\r'); 
     } 
     rd.close(); 
     Log.d(TAG, "Resultado: " + response.toString()); 
     return response.toString(); 
    }catch(IOException ex1){ 
     Log.e(TAG, "Error en conexión ex1"); 
     Log.d(TAG, ex1.toString()); 
     throw new Exception("Error en conexión"); 
    }catch(Exception ex2){ 
     Log.e(TAG, "Error en conexión ex2"); 
     throw new Exception("Error en conexión"); 
    } 
} 

回答

1

您必须首先检查您的图像是否可用或不在位图中。 打印您的编码字符串,只是看到它的来临与否。

如果是,那么让我知道我会将我的图像上传代码粘贴到服务器。