Visualize image from SQL database

I’m using android studio to create an app. The idea is to load a image … Read more Visualize image from SQL database

I’m using android studio to create an app. The idea is to load a image from a SQL database. But I’m having difficulties with it. I’m able to load the text parts which are in the database, but I’m unable to visualize the image. I also want to visualize the text and picture with the same button click.
The datatypes of my database are

ID = int

Name = char(10)

Filepath = char(150)

Picture = image

Down here you see my MainActivity.java.
I’ve tried many ways already to visualize it already, but can’t seem to find it working. I’ve also changed the getByte to getBlob but this doesn’t make a difference.

package com.allcodingtutorials.sqlconnection;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

public class MainActivity extends AppCompatActivity {

    EditText Rnumber;
    Button update;
    TextView name, path;
    ImageView picture;

    Connection connection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Rnumber = findViewById(R.id.textLoadData);
        update  = (Button) findViewById(R.id.btnGetData);
        name  = findViewById(R.id.Viewtext);
        path = findViewById(R.id.Viewtextpath);
        picture = findViewById(R.id.Viewimage);



        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String loadData = Rnumber.getText().toString();
                ConSQL con = new ConSQL();
                connection = con.conclass();
                if(con !=null){
                    try {
                        String sqlstatement="Select * From DatabasePictures where ID='"+ loadData +"'";
                        Statement smt = connection.createStatement();
                        ResultSet set = smt.executeQuery(sqlstatement);
                        while (set.next()){
                            name.setText(set.getString(2));
                            path.setText(set.getString(3));
                            picture.setImageBitmap(set.getByte(4));

                        }
                        //if(set.next()){
                        //    byte[] image = set.getByte(4);
                        //    Bitmap bitmap = BitmapFactory.decodeByteArray(image,0, image.length);
                        //   picture.setImageBitmap(bitmap);
                        //}
                        connection.close();
                    }

                    catch (Exception e)
                    {
                        Log.e("Error",e.getMessage());
                    }
                }
            }
        });
    }
}

Does anyone have some advice or tips how to solve this.

Source: JavaSript – Stack Overflow



Leave a Reply

Your email address will not be published. Required fields are marked *