How to store image in MS SQL Server database and retrieve it from your Android App?

preview_player
Показать описание
This video shows steps to create an Android App which connects to MS SQL Server Database.

It uses the MSSQL database to store the images in bytes array format. The respective data type in database server is VARBINARY(MAX).

It uses a couple of images stored in the emulator phone to demonstrate this App.

It reads those image files and then converts them to Bitmap and then to bytes array to store (insert) it in the database.

In the fetch method, once it receives the value of the VARBINARY data column from the database, it converts it back to the bytes array. Then it converts the bytes array back to bitmap. It uses an image view widget to display the bitmap image on the layout.

Complete source code and other details can be found in the below link:

However, the main Java code is copied below also for reference:

public class MainActivity extends AppCompatActivity {

private static String ip = "192.168.43.66";
private static String port = "1433";
private static String database = "testDatabase";
private static String username = "test";
private static String password = "test";
private static String url = "jdbc:jtds:sqlserver://"+ip+":"+port+"/"+database;

private Connection connection = null;

private TextView textView;
private EditText editTextIndex;
private EditText editTextFileName;
private ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
try {
} catch (ClassNotFoundException e) {
} catch (SQLException e) {
}
}

public void buttonInsert(View view){

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

try {

}
catch (Exception e){
}
}

public void buttonFetch(View view){
try {

}
catch (Exception e){
}
}
}

-
Рекомендации по теме
Комментарии
Автор

Wonderful video to feed image in SQL Server and fetch image

cditvinod
Автор

u`re really best! Thank you so much! I wish prosperity to ur channel👍

kiruhus
Автор

Thank you for your great video, helps a lot!

billwong
Автор

thanks so much, maybe this is the best implementation to saving image to sql
other people just using base64 encoding which is bad in performance

yohaneswilliam
Автор

Wonderful.
I have a note.
I have did the same steps but when I fetch the images from db it is very slow.
What should I do to make it faster

ibrahimalmesbahi
Автор

But it takes long time to retrieve images from sql server.

How can I develop it such as whatsapp to show data from local and save data on online Sql server

ibrahimalmesbahi
Автор

Hello, sir, I have a question
I have seen your video of recording but I need to make an activity in my application which when open start recording and when i click ok my recording will be sent to three numbers I give in start
Is this possible

dolly
Автор

I have created a website using Asp.net with SQL server. I stored Images and registration details of users. I need to know if I can also retrieve those images from my database.

thabangrapeane
Автор

How can I get the images inside a RecycleView? When I start the application I want to show the users that are already in the database, how can I do it?

pamelacastillo
Автор

Nice explain. But how can I do upload from ms sql server image to android studio ?
Pls. Hep. Thx a lot.

gta-servicesgta-services
Автор

My images are Stored in Ms Sql with image format.Not varbinary.So how can i get this images?

recepkuantum
Автор

Hello sir
I want to ask
I have an android application for safety of women that works fine now I want to use that code on locket, ring or watch. Can I? of yes then how

education_beauty
Автор

public void sqlButton(View view){
// String
if (entName.equals("")){
textView.setText("Add something");
}else{
try {
PreparedStatement preparedStatement = INTO patient_mst (pat_name) VALUES (?)");
preparedStatement.setInt(1,
preparedStatement.execute();
}
catch (Exception e){
e.printStackTrace();
}
}
}

rideforworld