如何使用 JDBC 驱动器将 MySql Table 图像 (BLOB) 加载到 ImageView
How to load MySql Table image (BLOB ) in to ImageView using JDBC Drive
我有一个项目正在进行中。在这个项目中,我试图通过 JDBC 驱动程序从 MySQL table 将登录用户图像获取到 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();
}
});
我有一个项目正在进行中。在这个项目中,我试图通过 JDBC 驱动程序从 MySQL table 将登录用户图像获取到 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();
}
});