简体   繁体   English

如何使用JDBC驱动器将MySql表图像(BLOB)加载到ImageView中

[英]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.

相关问题 无法使用Gallery Intent从GoogleStorageURI(Google Drive image uri)将图片加载到imageview中 - Unable to load image into imageview from GoogleStorageURI (Google Drive image uri) using Gallery Intent 如何将MYSQL中的Blob数据转换为Android ImageView - How to convert Blob data in MYSQL to Android ImageView 如何从mysql数据库加载图像并在android中的imageview中显示? - How to Load Image form mysql database and display in imageview in android? 如何在android的Imageview中设置BLOB图像? - how to set BLOB image in Imageview in android? 使用Picasso将图像加载到ImageView中 - Load Image into ImageView using Picasso 如何使用 Uri 和位图工厂将图像加载到 imageView - How to load an image into imageView using a Uri and Bitmap Factory 如何使用保存在数据库中的资源ID在imageview中加载图像 - How to load image in imageview using resource id which is saved in database 如何使用“ setImageURI”功能将图像从数据库加载到android ImageView中? - How to load image from database into android ImageView using “setImageURI” function? 如何使用滑动将GIF图像从可绘制文件夹加载到ImageView中? - How to load GIF image from drawable folder into ImageView using glide? 如何使用Picasso或Glide库将图像字符串路径加载到ImageView中? - How to load image string path into an ImageView using Picasso or Glide library?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM