[英]How to load MySql Table image (BLOB ) in to ImageView using JDBC Drive
I have a project working on. 我有一个正在进行的项目。 in this project I am trying to get login user image in to ImageView from MySQL table via JDBC Driver, in this case, image is loading one time and second time if I try to load the picture then system is giving an error, after the error again if I try to load the image then image is loading in to the ImageView. 在这个项目中,我试图通过JDBC驱动程序将登录用户图像从MySQL表获取到ImageView中,在这种情况下,如果我尝试加载图片,则图像将被加载一次和第二次,然后在错误后系统给出错误信息再次,如果我尝试加载图像,则图像正在加载到ImageView中。 Here I am attaching the code. 我在这里附加代码。
private class findpicture extends AsyncTask<String, String, String> {
String msg;
String searchtext= textView3.getText().toString();
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(getApplicationContext(), "Searching username", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
textView3.setText("");
}
@Override
protected String doInBackground(String... strings) {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)DriverManager.getConnection(url,user,pass);
if (con == null){
msg="Connection error...";
}else{
byte b[];
Blob blob;
Statement statement= (Statement) con.createStatement();
ResultSet resultSet = statement.executeQuery("select * from userlogin where name='"+searchtext+"'");
while (resultSet.next()){
blob = resultSet.getBlob("image");
b= blob.getBytes(1, (int)blob.length());
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
savedphoto.setImageBitmap(bitmap);
}
msg= "user image saved successfully...";
}
}catch (Exception e){
msg="unknown error..."+e;
System.out.println(e.toString());
}
return null;
}
}
Error Code android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 错误代码android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触摸其视图。
You have a Thread
conflict you need to recheck your calls and synchronized tasks there, Quick fix to your issue 您遇到了Thread
冲突,需要在此处重新检查呼叫和同步的任务,快速解决问题
runOnUiThread(new Runnable() {
@Override
public void run() {
new findpicture().execute();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.