convert blob image from mysql db to put it into an imageview

  • Replies:2
khayer fatima zahrae
  • Forum posts: 1

Mar 3, 2017, 11:55:38 PM via Website

hello !! i need your help !!
im a Beginner in android studio , i'm trying to get a blob image from mysql data base and to
put it in an imageview but i have to do some convertion ,
here is my code please help me to know where the problem is ,
when i run the emulator it's stop the app while asking to load the pic and put it
i guess the prblm is in the convertion !! help please!!:

public class SearchActivity extends Activity {
ImageView imgview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);
    imgview= (ImageView)this.findViewById(R.id.imageView);

}
public void getimg() throws android.database.SQLException, ClassNotFoundException{

    try{
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn= DriverManager.getConnection ("jdbc:mysql://localhost:3306/appbd","root","");
        Statement stmt=conn.createStatement();
        ResultSet rs=stmt.executeQuery("select * from imgtable");
        Blob b =rs.getBlob(1);
        int blobLength = (int) b.length();
        byte[] blobAsBytes = b.getBytes(1, blobLength);
        Bitmap btm =BitmapFactory.decodeByteArray(blobAsBytes,0,blobAsBytes.length);
        imgview.setImageBitmap(btm );
        conn.close();
    }
    catch (Exception e) {
        e.printStackTrace();
    }

}

}

Dragon School Vn

Reply
VKMOSES
  • Forum posts: 1

Oct 6, 2020, 11:03:27 AM via Website

Firstly, use your network IP address, and not "localhost"

and try again

Reply
Dragon School Vn
  • Forum posts: 1

Sep 1, 2023, 11:55:27 AM via Website

Thanks for this connvert: Blob to byte [] array:
Blob b =rs.getBlob(1);
int blobLength = (int) b.length();
byte[] blobAsBytes = b.getBytes(1, blobLength);

Reply