我有一个项目正在进行中。在这个项目中,我试图通过 JDBC 驱动程序将登录用户图像从 MySQL 表中获取到 ImageView,在这种情况下,图像加载一次,如果我尝试加载图片,则系统会在错误之后出现错误如果我再次尝试加载图像,则图像将加载到 ImageView 中。我在这里附上代码。
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.
最佳答案
你有一个 Thread
冲突,你需要重新检查那里的调用和同步任务,
快速解决您的问题
runOnUiThread(new Runnable() {
@Override
public void run() {
new findpicture().execute();
}
});
关于android - 如何使用 JDBC Drive 将 MySql 表图像 (BLOB) 加载到 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53866370/