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

Android 将位图图像转换为Base64(blob),并尝试在mysql连接中保存错误,android,Android,我将位图图像转换为Base64(blob),并尝试在mysql中保存,但“连接错误”。 android无法在mysql数据库中插入数据 protected void saveBitmap() { String image_str; try{ //save as byte ByteArrayOutputStream stream = new ByteArrayOutputStream();

我将位图图像转换为Base64(blob),并尝试在mysql中保存,但“连接错误”。 android无法在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();
         }
}
受保护的void saveBitmap(){
字符串图像;
试一试{
//另存为字节
ByteArrayOutputStream=新建ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG,90,stream);//压缩到您想要的格式。
byte[]byte_arr=stream.toByteArray();
image\u str=Base64.encodeToString(byte\u arr,Base64.DEFAULT);
////试验
ArrayList nvp=新的ArrayList();
nvp.add(新的BasicNameValuePair(“img”,image_str));
}
捕获(例外e)
{
Toast.makeText(MainActivity.this,“未转换为字节”,0.show();
}
试一试{
字符串url=”http://10.0.2.2/android/image.php";           
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
setEntity(新的UrlEncodedFormEntity(nvp));
HttpResponse HttpResponse=httpclient.execute(httppost);
}   
捕获(例外e)
{
Toast.makeText(MainActivity.this,“连接失败”,0).show();
}
}

将压缩类型更改为图像格式我在代码中使用JPEG格式,并将相同的字符串放入BasicNameValuePair[![enter image description here][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;
}

你在哪一行得到错误?另外,我想你指的是Base64而不是Byte64是的Base64,它们是http连接makingok中的异常,请在引发异常时发布Logcat输出。Logcat不是捕获异常并进行toast,而是删除toast,让应用程序崩溃并将Logcat添加到你的问题中。