0

I want to add some images into my SQL server. Is my problem from the insertion or getting image code?

For now the imageview that I retrieve the image is blank.

Here is my code for inserting the image:

else {
    DatabaseHelper mDatabaseHelper = new DatabaseHelper(MainActivity.this);
    Cursor cursor2 = mDatabaseHelper.GetPath();
    while (cursor2.moveToNext()) {

        Bitmap bitmap = BitmapFactory.decodeFile(cursor2.getString(0));
        ByteArrayOutputStream blob = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100 /* Ignored for PNGs */ , blob);
        byte[] bitmapdata = blob.toByteArray();
        String id = getIntent().getStringExtra("id");
        String query = "INSERT INTO StoresData VALUES('" + arabic + "','" + english + "','1','1','1','1','1','','" + lat + "','" + longi + "','" + mob + "','','','','','','','','0','','','','" + bitmapdata + "','" + user + "','" + passwords + "','0',NULL,'" + id + "')";
        Statement stmt = con.createStatement();
        stmt.executeUpdate(query);

        Cursor cursor = mDatabaseHelper.DeleteDataOfTableImagesAr();
        while (cursor.moveToNext()) {
            Toast.makeText(MainActivity.this, "Deleted images", Toast.LENGTH_LONG).show();
        }
    }
}

Retrieve the image back:

Blob blob = parkingList.get(position).getStoreicon();
if (blob != null) {
    byte[] byteArray = new byte[0];
    try {
        int length = (int) blob.length();
        byteArray = blob.getBytes(1, length);
    } catch (SQLException e) {
        e.printStackTrace();
    }
    Bitmap bm = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    intent.putExtra("storeicon", bm);
} else {

}

And yes I am getting the image into a listview.

Retrieving and getting the image are done in two different applications that is why I didn't use SQLite.

cela
  • 2,352
  • 3
  • 21
  • 43
  • I think you can best solve this by using a debugger. Set a breakpoint on this line "byteArray = blob.getBytes(1, length);" and make sure that you get to that point in the code. When you get there, make sure the length variable is not 0. If you don't get to that point it's likely that blob is null and you'll need to debug the "insert" portion of your code. – Josh Maag Jun 18 '20 at 20:46
  • @JoshMaag the length is working correctly i have set a system.out.println after the line and it is printing and working correctly. –  Jun 18 '20 at 21:02
  • @JoshMaag any ideas on what is wrong? I would really appreciate your help! –  Jun 18 '20 at 21:25
  • I think the next thing I would try it look at the first and last 5-10 bytes of bitmapdata in your input and byteArray in your retrieve portion and make sure they match. If they don't then it could be a problem with the way it's being saved in the database. If they do match, then it's probably something with the bitmap factory segment or assigning it. If that's the case, check to see that "bm" isn't null. Documentation for Bitmap factory says the return is: "The decoded bitmap, or _null_ if the image data could not be decoded" (emphasis mine). – Josh Maag Jun 19 '20 at 03:42
  • @JoshMaag i checked to see if it is null and you where right,it is. So what can i do next? –  Jun 19 '20 at 18:20
  • @JoshMaag But i found that my log prints `D/skia: --- Failed to create image decoder with message unimplemented` Any ideas? –  Jun 19 '20 at 18:31
  • Can you confirm that the 5-10 bytes at the beginning and end of the image are matching on both the insert and retrieval? If those are different then you need to look at the way you're saving pulling. If they match, then you probably need to add more specifications to the BitmapFactory. – Josh Maag Jun 19 '20 at 22:48

1 Answers1

0

I can't add a comment (too low reputation), but I'm thinking that

    int length = (int) blob.length();
    byteArray = blob.getBytes(1, length);

lenght is ok as you said, .getBytes start from 1 to lenght and store it in an array... But:

Bitmap bm = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length)

Shouldn't be

Bitmap bm = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length - 1)

? As lenght start from 1, arrays from 0.

Alrik
  • 100
  • 6
  • 1.Thanks a lot for your contribution and honesty! 2.I don't think that is the issue because i just tried it and it is still not working. but i have a big feeling that the code for inserting the image is the problem because i tried the same exact code of getting the picture in another app and it worked perfectly fine but i was inserting the image from a desktop app.so maybe the code for inserting is the problem?! –  Jun 18 '20 at 22:06
  • Ok, here I can comment :) I was looking at the insert: you used the " for the bitmapdata, but it's not a string, maybe is there the problem? – Alrik Jun 18 '20 at 22:36
  • Do you mean the " in the query string if yes then the " here is not used to indicate it is a string,it is used to stop the string.I mean it is a last semicolon of another one at first –  Jun 18 '20 at 22:42
  • Another thing, i am inserting the image from a uri and then converting it to bitmap and then to blob or byteArray –  Jun 18 '20 at 22:44
  • Sorry if I insist... but in SQL Server '" + bitmapdata + "' this is a String. About the uri, to check the step before (the download of the image and the conversion to bitmap) I wuold copy the func in another project and, instead of transform it in blob, show it in an Image View, just to be sure the problem is in the blob and not somewhere else... – Alrik Jun 18 '20 at 22:58
  • Oh you mean the ' okay one sec i will remove it and tell you the news! –  Jun 18 '20 at 23:00
  • So,now it gives me an exception `Exceptionsjava.sql.SQLException: Invalid SQL statement or JDBC escape, terminating ']' not found` Any ideas? –  Jun 18 '20 at 23:02
  • Also no worries when i get the image i show it into an imageview first before sending it to the server and it works fine! –  Jun 18 '20 at 23:05
  • Check this [Link](https://stackoverflow.com/questions/7620401/how-to-convert-byte-array-to-bitmap) IT's 1 am here.. time to sleep... hope the link helps!!! – Alrik Jun 18 '20 at 23:10