使用PHP将图像插入android中的mysql数据库

使用PHP将图像插入android中的mysql数据库,android,mysql,bitmap,Android,Mysql,Bitmap,我在使用PHP将图像保存到mysql时遇到问题。 我有一些代码,但有一些错误。 这是我的密码 Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.id.img_upload); //img_Photo.setImageBitmap(bitmap); ByteArrayOutputStream stream = new ByteArrayOutputStream();

我在使用PHP将图像保存到mysql时遇到问题。 我有一些代码,但有一些错误。 这是我的密码

  Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.id.img_upload);
         //img_Photo.setImageBitmap(bitmap);
         ByteArrayOutputStream stream = new ByteArrayOutputStream();
         bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.
         byte [] byte_arr = stream.toByteArray();
         image_str= Base64.encodeToString(byte_arr, 1);

         postParameters.add(new BasicNameValuePair("photo", image_str));
这里,
bitmap.compress(bitmap.CompressFormat.PNG,90,stream)始终为空。

我的代码错误,或者我应该修复哪个部分。给我一些建议。

首先将图像转换为基本的64字节数组编码字符串。之后,将其发送到php。在服务器端提取。然后将该字符串存储在MySQL中。之后,将该字符串发送到android客户端。提取图像字符串并使用Base64解码进行解码。之后,您将得到字节数组,您可以简单地显示在图像视图中。我将展示一些代码供您参考

      String imagedata = Base64.encodeToString(thumbnailArray,Base64.DEFAULT);
            mJobject.put("imagebyte",imagedata);
              mJArray.put(mJobject);

            JSONArray json=new JSONArray(response);
            JSONObject jo = null;

           imageArray=new String[json.length()];

         imageArray[i]=jo.getString("imageid");

          completeImage= Base64.decode(imageArray[0],Base64.DEFAULT); 
Bitmap bitmap = BitmapFactory.decodeByteArray(completeImage , 0, completeImage.length);

更改您的线路如下:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.img_upload);
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
如果要从
ImageButton
获取图像,请尝试以下操作:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.img_upload);
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();

xml.try
Base64.encodeBytes(byte\u arr)中的Imagebutton无法更改Base64.encodeBytes(byte\u arr);在我的应用程序中,Base64没有encodeBytes()@用户3032822检查我的答案并尝试相应地更改。img_上载为Imagebutton.:)您无法通过这种方式从图像按钮获取图像。您正在尝试从ImageButton获取图像吗?是的,它应该仅从drawable获取,而不是从视图获取。是的,我必须将图像从gallery存储到mysql数据库。在这种情况下,我还会在imagebutton上显示来自gallery的图像。查看本教程,它将帮助您@用户3032822