0

I created a DB in mysql workbunch and i stor picture in it as as type blob.
Now I want to use this picture in my app,

Class.forName("com.mysql.jdbc.Driver");
                try(Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sys", "root", "Elior204")){
                    Statement stmt = con.createStatement();
                    ResultSet rs = stmt.executeQuery("SELECT img FROM Sentences where id=1");
                       ImageView image = (ImageView) findViewById(R.id.imageView1);
                    Blob b =rs.getBlob(1);
                    int blobLength = (int) b.length();
                    byte[] blobAsBytes = b.getBytes(1, blobLength);
                    Bitmap btm =BitmapFactory.decodeByteArray(blobAsBytes,0,blobAsBytes.length);
                    image.setImageBitmap(btm );
                    con.close();

The question is, how I continue?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Elior Sastiel
  • 1,074
  • 2
  • 10
  • 21

1 Answers1

0
java.sql.Blob blob = rs.getBlob(1);
byte[] imageContents = blob.getBytes(1, new Integer(blob.length()).intValue());

Now you can manipulate it as you wish.

Per your comment, you want to display it in your application, to accomplish this, add an ImageView to your layout and use the following code:

 image = (ImageView) findViewById(R.id.imageView1);

 button = (Button) findViewById(R.id.btnChangeImage);
 button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
          java.io.File tmpFileForImage = java.io.File.createTemporaryFile("image", ".png"); // or whatever your extension happens to be
          java.io.BufferedOutputStream bos = new java.io.BufferedOutputStream(new java.io.FileOutputStream(tmpFileForImage);
          bos.write(imageContents);
          bos.close();
          image.setImageResource(tmpFileForImage.getAbsolutePath());
    });

Untested, but should get you on the right track. If you need further assistance, leave another comment.

hd1
  • 33,938
  • 5
  • 80
  • 91