This is a continuation of my previous post on Android Storing Images in MS SQL Server. This one explains how you can retrieve the image back into Android Imageview.
Basically the code is simple. You have to just decode the base64 value back.
byte[] decodeString = Base64.decode(“BASE64 VALUE HERE”, Base64.DEFAULT);
Bitmap decodebitmap = BitmapFactory.decodeByteArray(decodeString,
0, decodeString.length);
img.setImageBitmap(decodebitmap);
The Application
Make a new Layout xml file in your res > layout folder with name fetchimage.xml and edit it as following :
fetchimage.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#1abc9c" android:orientation="vertical" android:weightSum="11" > <TextView android:layout_width="wrap_content" android:layout_height="0dp" android:layout_gravity="center" android:layout_weight="1" android:padding="10dp" android:text="ANDROID FETCH IMAGE" android:textColor="#fff" android:textSize="15sp" /> <EditText android:id="@+id/edtid" android:layout_width="256dp" android:layout_height="0dp" android:layout_gravity="center" android:layout_weight="1" android:background="@android:drawable/editbox_background_normal" android:hint="ENTER ID HERE" android:textColor="#000" android:typeface="monospace" /> <ProgressBar android:id="@+id/pbbar" android:layout_width="20dp" android:layout_height="0dp" android:layout_gravity="center" android:layout_marginLeft="5dp" android:layout_weight="1" /> <Button android:id="@+id/btnfetch" android:layout_width="256dp" android:layout_height="0dp" android:background="@android:drawable/btn_default" android:layout_gravity="center" android:layout_weight="1" android:text="FETCH" /> <ImageView android:id="@+id/imageview" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="6" android:src="@drawable/img11" /> <TextView android:id="@+id/lblpath" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_gravity="center" android:layout_marginTop="5dp" android:layout_weight="2" android:padding="10dp" android:text="v xcvcxvcvcvxcvxcvxcvxcvxcvxc v xcvcxvcvcvxcvxcvxcvxcvxcvxc v xcvcxvcvcvxcvxcvxcvxcvxcvxc" android:textColor="#fff" android:textSize="13sp" /> </LinearLayout>
Make a new java Class with name FetchImage.java and edit as following :
FetchImage.java
package com.example.temp; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import android.annotation.SuppressLint; import android.app.Dialog; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.os.Bundle; import android.os.StrictMode; import android.support.v7.app.ActionBarActivity; 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.ProgressBar; import android.widget.TextView; /** * Created by abc on 13-Jul-15. */ public class FetchImage extends ActionBarActivity { public static final int requestcode = 1; ImageView img; TextView lblpath; Dialog m; EditText edtid; Button btnfetch; ProgressBar pbbar; byte[] byteArray; String call, db, un, passwords; String id; Connection connect; ResultSet rs; @SuppressLint("NewApi") private Connection CONN(String _user, String _pass, String _DB, String _server) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() .permitAll().build(); StrictMode.setThreadPolicy(policy); Connection conn = null; String ConnURL = null; try { Class.forName("net.sourceforge.jtds.jdbc.Driver"); ConnURL = "jdbc:jtds:sqlserver://" + _server + ";" + "databaseName=" + _DB + ";user=" + _user + ";password=" + _pass + ";"; // c: // :c conn = DriverManager.getConnection(ConnURL); } catch (SQLException se) { Log.e("ERRO", se.getMessage()); } catch (ClassNotFoundException e) { Log.e("ERRO", e.getMessage()); } catch (Exception e) { Log.e("ERRO", e.getMessage()); } return conn; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fetchimage); img = (ImageView) findViewById(R.id.imageview); lblpath = (TextView) findViewById(R.id.lblpath); edtid = (EditText) findViewById(R.id.edtid); btnfetch = (Button) findViewById(R.id.btnfetch); pbbar = (ProgressBar) findViewById(R.id.pbbar); pbbar.setVisibility(View.GONE); call = "192.168.0.100"; un = "sa"; passwords = "123"; db = "mydb1"; btnfetch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { id = edtid.getText().toString(); Fetch f = new Fetch(); f.execute(""); } }); } private class Fetch extends AsyncTask<String, String, String> { String z = " "; @Override protected void onPreExecute() { pbbar.setVisibility(View.VISIBLE); } @Override protected String doInBackground(String... params) { // TODO Auto-generated method stub String truckstring = "select * from imgtbl where ID=" + id; try { connect = CONN(un, passwords, db, call); Statement stmt = connect.createStatement(); rs = stmt.executeQuery(truckstring); if (rs != null && rs.next()) { z = rs.getString("Images"); } } catch (Exception e) { System.out.print("err"); e.printStackTrace(); z = "No such ID"; } return z; } @Override protected void onPostExecute(String rs) { byte[] decodeString = Base64.decode(rs, Base64.DEFAULT); Bitmap decodebitmap = BitmapFactory.decodeByteArray(decodeString, 0, decodeString.length); img.setImageBitmap(decodebitmap); pbbar.setVisibility(View.GONE); } } }