Smolina Fezaphitsh
Smolina Fezaphitsh

Reputation: 347

encode and decode base64 excpetion storing to sql server 2008 r2

I have an android client, I want to send image from my android to my server, i convert the image to byte array, then they byte array to base64, i send the base64 string to server, i decode it in the server then stored it in my sql server 2008 r2.

my problem

I can't store the string to my database then retrieve it correctly, I didn't get any exception and I get result but it seems it is the wrong result.

i conclude that by doing this.

1- i send the base64 from android to server and saved the string retrieved in a static variable. 2- i asked the server to retrive the static string, i get the image in the android back. 3- but when i asked the server to retrieve the image that i supposed i saved it in the database i got wrong image ,actullly i get result but this result can't be decoded again.

I will tell you my code for insert and retrive the image in database, pleaes tell me what am i doing wrong,

Stored to database

void uploadImage(String image) {
Connection con = Database.getConnection();
        CallableStatement callableStatement = null;
        try {
            callableStatement = con
                    .prepareCall("{call insertRestaurantFoodImage(?,?)}");
            ByteArrayInputStream b = new ByteArrayInputStream(
                    stringImage.getBytes());
            callableStatement.setInt(1, ID);
            ;
            callableStatement.setBinaryStream(2, b,
                    stringImage.getBytes().length);
            callableStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
}

the stored procedure to insert the image is

ALTER PROCEDURE dbo.insertRestaurantFoodImage
    (
    @restaurantFoodID INT,
    @image VARBINARY(MAX)
    )
AS
BEGIN
    SET NOCOUNT ON
    UPDATE Food_Restaurant
    SET [image] = @image
    WHERE ID = @restaurantFoodID
END

Retrieve from database

Connection con = Database.getConnection();
            CallableStatement callableStatement = null;
            try {
                callableStatement = con
                        .prepareCall("{call getRestaurantFoodImage(?,?)}");
                callableStatement.setInt(1, getID());
                callableStatement.registerOutParameter(2,
                        java.sql.Types.VARBINARY);
                callableStatement.execute();
                byte[] bytes = callableStatement.getBytes(2);
                image = new String(bytes);
            } catch (SQLException e) {
                e.printStackTrace();
            }

the stored procedure to retrive from database is :

ALTER PROCEDURE dbo.getRestaurantFoodImage
(
    @foodRestaurantID INT,
    @image VARBINARY(MAX) OUTPUT
    )
AS
BEGIN
    SET NOCOUNT ON
    SELECT @image = [image]
    FROM Food_Restaurant
    WHERE ID = @foodRestaurantID
END

maybe i talked too much, but in the previus question, a user told me that i have to not just put the code but i have to put my problem and what i am doing.

for those uses who asked me about how to encode and decode in android here is it:

encode

private byte[] getBytesFromBitmap(Bitmap bitmap) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.PNG, 70, stream);
        return stream.toByteArray();
    }

    private String getBase64(Bitmap bitmap) {
        String imgString = Base64.encodeToString(getBytesFromBitmap(bitmap),
                Base64.NO_WRAP);
        return imgString;
    }

decode

String retrievedBase63 = client2
                .getBaseURI("restaurantFoods/OneFood/" + 5 + "/getImage");
        //Log.d("image", image);
        byte[] decodedString = Base64.decode(retrievedBase63, Base64.DEFAULT);
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0,
                decodedString.length);

Upvotes: 0

Views: 2798

Answers (1)

Mark Rotteveel
Mark Rotteveel

Reputation: 109015

As I also commented (and responded) on jdbc stored png image at sql server 2008 r2 gives not complete data older version of SQL Server had a limit of 8kb for varbinary and varchar. Either the driver simply doesn't support longer for getBytes or getString, or you might be using an older JDBC driver. Another possibility is that you need to register the OUT parameter as java.sql.types.LONGVARBINARY (or LONGVARCHAR).

In any case (also in the other question), I am wondering why you are first encoding in base64 before storing your data. Just store the raw bytes in a VARBINARY.

Upvotes: 1

Related Questions