Comment récupérer l'image varbinary (max) de SQL Server dans android?

Je développe une application Android dans laquelle je veux récupérer l'image stockée dans la database du server SQL comme indiqué dans la capture d'écran:

entrez la description de l'image ici

J'ai écrit un code pour cela mais l'application récupérera seulement la valeur de colonne de titre et montrera avec succès dans le textview mais dans l'imageview rien ne s'affiche. Chaque sorte d'aide est appréciée. Voici mon code pour ça:

public class Menu_listn extends Fragment implements View.OnClickListener { Connection con; TextView t; ImageView img; Ssortingng un,pass,db,ip,in; private Ssortingng abc; @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); getActivity().setTitle("abc"); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { final View InputFragmentView = inflater.inflate(R.layout.menu_1, container, false); t = (TextView) InputFragmentView.findViewById(R.id.track_title); img=(ImageView) InputFragmentView.findViewById(R.id.track_image); ip = "192.168.***.**"; in="SQLEXPRESS"; db = "Testaudio"; un = "**"; pass = "****"; Check check1 = new Check();// this is the Asynctask, which is used to process in background to reduce load on app process check1.execute(""); return InputFragmentView; } public Ssortingng getAbc() { return abc; } public void setAbc(Ssortingng abc) { this.abc = abc; } public class Check extends AsyncTask<Ssortingng,Ssortingng,Ssortingng> { Ssortingng z = ""; Boolean isSuccess = false; @Override protected void onPreExecute() { } @Override protected void onPostExecute(Ssortingng r) { if(isSuccess) { Toast.makeText(getActivity() , "Successfull" , Toast.LENGTH_SHORT).show(); t.setText(getAbc()); byte[] decodeSsortingng = Base64.decode(r, Base64.DEFAULT); Bitmap decodebitmap = BitmapFactory.decodeByteArray(decodeSsortingng, 0, decodeSsortingng.length); img.setImageBitmap(decodebitmap); } } @Override protected Ssortingng doInBackground(Ssortingng... params) { try { con = connectionclass(un, pass, db, ip,in); if (con == null) { z = "Check Your Internet Access!"; } else { Ssortingng query = "select * from getImg"; Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(query); if(rs != null && rs.next()) { z = "successful"; isSuccess=true; setAbc(rs.getSsortingng(3)); z=rs.getSsortingng(2); } con.close(); } } catch (Exception ex) { isSuccess = false; z = ex.getMessage(); } return z; } } @SuppressLint("NewApi") public Connection connectionclass(Ssortingng user, Ssortingng password, Ssortingng database, Ssortingng server,Ssortingng instance) { SsortingctMode.ThreadPolicy policy = new SsortingctMode.ThreadPolicy.Builder().permitAll().build(); SsortingctMode.setThreadPolicy(policy); Connection connection = null; Ssortingng ConnectionURL = null; try { Class.forName("net.sourceforge.jtds.jdbc.Driver"); ConnectionURL = "jdbc:jtds:sqlserver://" + server + "/" + database + ";instance=" + instance + ";user=" + user + ";password=" + password + ";"; connection = DriverManager.getConnection(ConnectionURL); } catch (SQLException se) { Log.e("error here 1 : ", se.getMessage()); t.setText(se.getMessage()); } catch (ClassNotFoundException e) { Log.e("error here 2 : ", e.getMessage()); t.setText(e.getMessage()); } catch (Exception e) { Log.e("error here 3 : ", e.getMessage()); t.setText(e.getMessage()); } return connection; } } 

Voici le code que j'ai utilisé pour mon application

Ce code prendra une image d'url et convertira en un tableau d'octets

 byte[] logoImage = getLogoImage(IMAGEURL); private byte[] getLogoImage(Ssortingng url){ try { URL imageUrl = new URL(url); URLConnection ucon = imageUrl.openConnection(); InputStream is = ucon.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(500); int current = 0; while ((current = bis.read()) != -1) { baf.append((byte) current); } return baf.toByteArray(); } catch (Exception e) { Log.d("ImageManager", "Error: " + e.toSsortingng()); } return null; } 

Pour save l'image sur db j'ai utilisé ce code.

  public void insertUser(){ SQLiteDatabase db = dbHelper.getWritableDatabase(); Ssortingng delSql = "DELETE FROM ACCOUNTS"; SQLiteStatement delStmt = db.comstackStatement(delSql); delStmt.execute(); Ssortingng sql = "INSERT INTO ACCOUNTS (account_id,account_name,account_image) VALUES(?,?,?)"; SQLiteStatement insertStmt = db.comstackStatement(sql); insertStmt.clearBindings(); insertStmt.bindSsortingng(1, Integer.toSsortingng(this.accId)); insertStmt.bindSsortingng(2,this.accName); insertStmt.bindBlob(3, this.accImage); insertStmt.executeInsert(); db.close(); 

}

Pour récupérer l'image, c'est le code que j'ai utilisé.

 public Account getCurrentAccount() { SQLiteDatabase db = dbHelper.getWritableDatabase(); Ssortingng sql = "SELECT * FROM ACCOUNTS"; Cursor cursor = db.rawQuery(sql, new Ssortingng[] {}); if(cursor.moveToFirst()){ this.accId = cursor.getInt(0); this.accName = cursor.getSsortingng(1); this.accImage = cursor.getBlob(2); } if (cursor != null && !cursor.isClosed()) { cursor.close(); } db.close(); if(cursor.getCount() == 0){ return null; } else { return this; } } 

Enfin pour charger cette image dans une imageview

 logoImage.setImageBitmap(BitmapFactory.decodeByteArray( currentAccount.accImage, 0,currentAccount.accImage.length));