我正在开发一个Android应用程序,我想在其中检索存储在SQL Server数据库中的图像,如屏幕截图所示:
我已为此编写了代码但应用程序只会检索标题列值并成功显示在textview中,但在imageview中没有显示任何内容。非常感谢各种帮助。以下是我的代码:
public class Menu_listen extends Fragment implements View.OnClickListener {
Connection con;
TextView t;
ImageView img;
String un,pass,db,ip,in;
private String 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 String getAbc() {
return abc;
}
public void setAbc(String abc) {
this.abc = abc;
}
public class Check extends AsyncTask<String,String,String>
{
String z = "";
Boolean isSuccess = false;
@Override
protected void onPreExecute()
{
}
@Override
protected void onPostExecute(String r)
{
if(isSuccess)
{
Toast.makeText(getActivity() , "Successfull" , Toast.LENGTH_SHORT).show();
t.setText(getAbc());
byte[] decodeString = Base64.decode(r, Base64.DEFAULT);
Bitmap decodebitmap = BitmapFactory.decodeByteArray(decodeString, 0, decodeString.length);
img.setImageBitmap(decodebitmap);
}
}
@Override
protected String doInBackground(String... params)
{
try
{
con = connectionclass(un, pass, db, ip,in);
if (con == null)
{
z = "Check Your Internet Access!";
}
else
{
String query = "select * from getImg";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
if(rs != null && rs.next())
{
z = "successful";
isSuccess=true;
setAbc(rs.getString(3));
z=rs.getString(2);
}
con.close();
}
}
catch (Exception ex)
{
isSuccess = false;
z = ex.getMessage();
}
return z;
}
}
@SuppressLint("NewApi")
public Connection connectionclass(String user, String password, String database, String server,String instance) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection connection = null;
String 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;
}
}
答案 0 :(得分:1)
这里是我用于我的应用程序的代码
此代码将从url获取图像并将其转换为字节数组
byte[] logoImage = getLogoImage(IMAGEURL);
private byte[] getLogoImage(String 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.toString());
}
return null;
}
要将图像保存到db,我使用了此代码。
public void insertUser(){
SQLiteDatabase db = dbHelper.getWritableDatabase();
String delSql = "DELETE FROM ACCOUNTS";
SQLiteStatement delStmt = db.compileStatement(delSql);
delStmt.execute();
String sql = "INSERT INTO ACCOUNTS (account_id,account_name,account_image) VALUES(?,?,?)";
SQLiteStatement insertStmt = db.compileStatement(sql);
insertStmt.clearBindings();
insertStmt.bindString(1, Integer.toString(this.accId));
insertStmt.bindString(2,this.accName);
insertStmt.bindBlob(3, this.accImage);
insertStmt.executeInsert();
db.close();
}
要检索图像,这是我使用过的代码。
public Account getCurrentAccount() {
SQLiteDatabase db = dbHelper.getWritableDatabase();
String sql = "SELECT * FROM ACCOUNTS";
Cursor cursor = db.rawQuery(sql, new String[] {});
if(cursor.moveToFirst()){
this.accId = cursor.getInt(0);
this.accName = cursor.getString(1);
this.accImage = cursor.getBlob(2);
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
db.close();
if(cursor.getCount() == 0){
return null;
} else {
return this;
}
}
最后将此图片加载到imageview
logoImage.setImageBitmap(BitmapFactory.decodeByteArray( currentAccount.accImage,
0,currentAccount.accImage.length));