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

Описание к видео How to store image in MS SQL Server database and retrieve it from your Android App?

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.

I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: [email protected]

Complete source code and other details can be found in the below link:
https://programmerworld.co/android/ho...


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

package com.programmerworld.imagemssqlapp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import java.io.ByteArrayOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MainActivity extends AppCompatActivity {

private static String ip = "192.168.43.66";
private static String port = "1433";
private static String Classes = "net.sourceforge.jtds.jdbc.Driver";
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) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);

ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.INTERNET}, PackageManager.PERMISSION_GRANTED);

textView = findViewById(R.id.textViewStatus);
editTextIndex = findViewById(R.id.editTextNumber);
editTextFileName = findViewById(R.id.editTextFileName);
imageView = findViewById(R.id.imageView);

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
Class.forName(Classes);
connection = DriverManager.getConnection(url, username,password);
textView.setText("SUCCESS");
} catch (ClassNotFoundException e) {
e.printStackTrace();
textView.setText("ERROR");
} catch (SQLException e) {
e.printStackTrace();
textView.setText("FAILURE");
}
}

public void buttonInsert(View view){
String stringFilePath = Environment.getExternalStorageDirectory().getPath()+"/Download/"+
editTextFileName.getText().toString()+".jpeg";

Bitmap bitmap = BitmapFactory.decodeFile(stringFilePath);

imageView.setImageBitmap(bitmap);

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, byteArrayOutputStream);
byte[] bytesImage = byteArrayOutputStream.toByteArray();

try {
PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO TEST_TABLE (C2, Image) VALUES (?,?)");
preparedStatement.setInt(1, Integer.valueOf(editTextIndex.getText().toString()));
preparedStatement.setBytes(2, bytesImage);

preparedStatement.execute();
}
catch (Exception e){
e.printStackTrace();
}
}

public void buttonFetch(View view){
try {
Statement statement = connection.createStatement();

ResultSet resultSet = statement.executeQuery("SELECT Image from TEST_TABLE where C2="+editTextIndex.getText().toString()+";");
resultSet.next();
byte[] bytesImageDB = resultSet.getBytes(1);
Bitmap bitmapImageDB = BitmapFactory.decodeByteArray(bytesImageDB, 0, bytesImageDB.length);

imageView.setImageBitmap(bitmapImageDB);
}
catch (Exception e){
e.printStackTrace();
}
}
}

-

Комментарии

Информация по комментариям в разработке