0

I have already stored some data in the SQL Server database with this code

_bitmap = BitmapFactory.decodeFile(path);
byteArrayOutputStream = new ByteArrayOutputStream();
_bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
bytesImage = byteArrayOutputStream.toByteArray();
image = Base64.encodeToString(bytesImage, Base64.DEFAULT);

String query =
    "INSERT INTO EloanApp(SeqID,AppImage) VALUES ('" + seqid + "','" + image + "')";
Statement stmt = con.createStatement();
stmt.executeUpdate(query);

And to make it sure that if it is successfully uploaded the image, I tried to export the image stored in my database and opened it, the photo viewer, shows "It looks like we don't support this file format." what seems could be the problem here?

PS.

I exported the image with this method

Simple Image Import and Export Using T-SQL for SQL Server

Thom A
  • 88,727
  • 11
  • 45
  • 75
JeffyO
  • 81
  • 10
  • 2
    It seems very strange that you are storing you image as a string in the database... why not store as `varbinary` and use a parameter to pass the data to the database. – Dale K Aug 10 '23 at 03:14
  • @DaleK it is stored as binary data – JeffyO Aug 10 '23 at 03:58
  • 2
    No, you are base64 encoding it, then storing it as a string. At least according to your code. – Dale K Aug 10 '23 at 03:58
  • 1
    @DaleK dude thanks for the tip. I've solved my problem. Ughh so silly. – JeffyO Aug 10 '23 at 05:15
  • 1
    I removed the answer from your question, as you have correctly posted it as a solution - which you can self-accept after some time period. – Dale K Aug 10 '23 at 05:38

1 Answers1

0

It turned out that removing this line

image = Base64.encodeToString(bytesImage, Base64.DEFAULT);

and using a PreparedStatement to execute my query

PreparedStatement preparedStatement = con.prepareStatement("INSERT INTO EloanApp(SeqID,AppImage) VALUES(?,?);

preparedStatement.setString(1, seq_id);
preparedStatement.setBytes(2, bytesImage);
preparedStatement.execute();

solved the issue!

Dale K
  • 25,246
  • 15
  • 42
  • 71
JeffyO
  • 81
  • 10