Advertisement
Guest User

Untitled

a guest
Aug 25th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. Class.forName("com.mysql.jdbc.Driver");
  2. try(Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sys", "root", "Elior204")){
  3. Statement stmt = con.createStatement();
  4. ResultSet rs = stmt.executeQuery("SELECT img FROM Sentences where id=1");
  5. ImageView image = (ImageView) findViewById(R.id.imageView1);
  6. Blob b =rs.getBlob(1);
  7. int blobLength = (int) b.length();
  8. byte[] blobAsBytes = b.getBytes(1, blobLength);
  9. Bitmap btm =BitmapFactory.decodeByteArray(blobAsBytes,0,blobAsBytes.length);
  10. image.setImageBitmap(btm );
  11. con.close();
  12.  
  13. java.sql.Blob blob = rs.getBlob(1);
  14. byte[] imageContents = blob.getBytes(1, new Integer(blob.length()).intValue());
  15.  
  16. image = (ImageView) findViewById(R.id.imageView1);
  17.  
  18. button = (Button) findViewById(R.id.btnChangeImage);
  19. button.setOnClickListener(new OnClickListener() {
  20.  
  21. @Override
  22. public void onClick(View arg0) {
  23. java.io.File tmpFileForImage = java.io.File.createTemporaryFile("image", ".png"); // or whatever your extension happens to be
  24. java.io.BufferedOutputStream bos = new java.io.BufferedOutputStream(new java.io.FileOutputStream(tmpFileForImage);
  25. bos.write(imageContents);
  26. bos.close();
  27. image.setImageResource(tmpFileForImage.getAbsolutePath());
  28. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement