将位图图像转换为Base64(blob),并尝试在连接中保存mysql错误

时间:2014-09-05 18:53:21

标签: android

我将位图图像转换为Base64(blob),并尝试保存在mysql中但是"错误连接"。 机器人-无法到嵌件中数据的MySQL数据库

protected void saveBitmap() {

     String image_str;


         try{
                //save as byte
             ByteArrayOutputStream stream = new ByteArrayOutputStream();
             image.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format yo want.
             byte [] byte_arr = stream.toByteArray();
             image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT);
                 ////test
             ArrayList<NameValuePair> nvp = new ArrayList<NameValuePair>(); 
             nvp.add(new BasicNameValuePair("img",image_str));
         }
         catch(Exception e)
         {
             Toast.makeText(MainActivity.this, "Not converted to byte", 0).show();
         }

         try{
             String url ="http://10.0.2.2/android/image.php";           
             HttpClient httpclient = new DefaultHttpClient();
             HttpPost httppost = new HttpPost(url);
             httppost.setEntity(new UrlEncodedFormEntity(nvp));
             HttpResponse httpresponse=httpclient.execute(httppost);

         }   
         catch(Exception e)
         {
             Toast.makeText(MainActivity.this, "Connection failed", 0).show();
         }
}

1 个答案:

答案 0 :(得分:1)

将您的压缩类型更改为您的图像格式我在我的代码中使用JPEG,并将相同的字符串放在BasicNameValuePair [![在此处输入图像描述] [1]] [1],同时将列名添加到数据库中。为此你可以看到我的下面代码

public String BitMapToString(Bitmap bitmap){

    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG,10, baos);
    byte [] b=baos.toByteArray();
    String temp= Base64.encodeToString(b, Base64.DEFAULT);

    return temp;
}
相关问题