在base64中对图像进行编码并发送到服务器

时间:2012-01-27 00:14:38

标签: android base64 jpeg

我在发送图像base64编码的字符串时遇到问题。我在服务器中始终存在相同的错误“Unsupported marker type 0xb7”,并且响应始终为“java.io.FileNotFoundException”

/**
 * 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 个答案:

答案 0 :(得分:1)

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

如果是,请告诉我,我会将我的图片上传代码粘贴到服务器上。