Sending Image to the MySQL database with Android

后端 未结 1 1986
鱼传尺愫
鱼传尺愫 2020-12-15 14:52

I have the current application which sends and retrieves data from a MySQL database but the information so far I was transferring was String. How can I update the code below

相关标签:
1条回答
  • 2020-12-15 15:05

    If you want to send Image on Server! You have to change it in Base 64 string.

    For saving it in Database! You have to convert it in Blob type!

    try this code:

            Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);          
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.
            byte [] byte_arr = stream.toByteArray();
            String image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT);
    

    And pass this as a parameter just like you are adding other parameters in request!

    params.add(new BasicNameValuePair("image",image_str));
    

    I hope this will help! See this link for further detail.

    0 讨论(0)
提交回复
热议问题