这是获取单个图像并在图像视图中显示的代码。
public class Database_demo extends Activity {
public static final int DIALOG_DOWNLOAD_JSON_PROGRESS = 0;
int[] flags;
String strImageName;
int n = 10000;
String[] mySecondStringArray = new String[n];
String[] strArray;
HashMap<String, String> hm;
List<HashMap<String, String>> aList;
InputStream is = null;
String result = "";
JSONObject jArray = null;
private String Qrimage;
private Bitmap bmp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news);
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(
"http://192.168.1.50/XXXX/getimage.php");
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
jArray = new JSONObject(result);
JSONArray json = jArray.getJSONArray("tablename");
for (int i = 0; i < json.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = json.getJSONObject(i);
Qrimage = e.getString("imagefieldname");
System.out.println(Qrimage);
byte[] qrimage = Base64.decode(Qrimage.getBytes(), i);
System.out.println(qrimage);
bmp = BitmapFactory.decodeByteArray(qrimage, 0, qrimage.length);
ImageView imageview = (ImageView) findViewById(R.id.flag);
imageview.setImageBitmap(bmp);
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
php文件
<?php
$host="localhost"; //replace with database hostname
$username="root"; //replace with database username
$password=""; //replace with database password
$db_name="databasename"; //replace with database name
$con=mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql = "SELECT * FROM tablename";
$result1 = mysql_query($sql);
$json = array();
if(mysql_num_rows($result1)){
while($row=mysql_fetch_assoc($result1)){
$json['tablename'][]=$row;
}
}
mysql_close($con);
echo json_encode($json);
?>