我有带有图像 blob 的 MySQL 表,现在我想检索它们并将它们写入一个数组,以便稍后在 gridview 中显示它们。但我的问题是while循环永远不会结束。
这里有一些关于发生了什么的日志文件。
06-13 23:16:24.550 29200-29224/com.test.app I/System.out: Getting Image with id:11
06-13 23:16:24.555 29200-29224/com.test.app I/System.out: Getting Image with id:12
06-13 23:16:24.760 29200-29225/com.test.app I/System.out: com.mysql.jdbc.ResultSet@41860510
06-13 23:16:24.765 29200-29225/com.test.app I/System.out: Getting Image with id:11
06-13 23:16:24.780 29200-29225/com.test.app I/System.out: Getting Image with id:12
06-13 23:16:25.095 29200-29226/com.test.app I/System.out: com.mysql.jdbc.ResultSet@41fe2788
06-13 23:16:25.105 29200-29226/com.test.app I/System.out: Getting Image with id:11
06-13 23:16:25.110 29200-29226/com.test.app I/System.out: Getting Image with id:12
06-13 23:16:25.450 29200-29222/com.test.app I/System.out: com.mysql.jdbc.ResultSet@41809328
06-13 23:16:25.455 29200-29222/com.test.app I/System.out: Getting Image with id:11
06-13 23:16:25.460 29200-29222/com.test.app I/System.out: Getting Image with id:12
这是我的应用程序的代码。
public class Activity2CameraView extends Activity {
private static final String driver = "com.mysql.jdbc.Driver";
private static final String url = "jdbc:mysql://XXXX";
private static final String user = "XXXX";
private static final String pass = "XXXX";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new getImage().execute();
}
private class getImage extends AsyncTask<Void, Void, ArrayList<HashMap<String, Bitmap>>> {
ArrayList<HashMap<String, Bitmap>> mylist = new ArrayList<HashMap<String, Bitmap>>();
@Override
protected ArrayList<HashMap<String, Bitmap>> doInBackground(Void... voids) {
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inDither = false;
opt.inPreferredConfig = Bitmap.Config.RGB_565;
HashMap map = new HashMap<String, Bitmap>();
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, pass);
PreparedStatement stat = conn.prepareStatement("SELECT id, imageblob FROM photos");
ResultSet rset = stat.executeQuery();
System.out.println(rset);
rset.beforeFirst();
while (rset.next()) {
ByteArrayInputStream inputStream = new ByteArrayInputStream(rset.getBytes("imageblob"));
Bitmap bm = BitmapFactory.decodeStream(inputStream);
int id = rset.getInt("id");
map.put("id", id);
map.put("image", bm);
mylist.add(map);
System.out.println("Getting Image with id:" + id);
}
rset.close();
stat.close();
conn.close();
return mylist;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(ArrayList<HashMap<String, Bitmap>> result) {
setContentView(R.layout.cam_main);
String[] from = {"id", "image"};
int[] to = {R.id.id, R.id.image_db};
SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), result, R.layout.image_grid, from, to);
// Getting a reference to gridview of MainActivity
GridView gridView = (GridView) findViewById(R.id.gridview_cam);
gridView.setAdapter(adapter);
new getImage().execute();
}
}
}