我正在从 java 将数据发布到一个 URL,该 URL 用于从 Web 服务器上的 MySQL 字段PHP
返回一个json_encode(d)
blob。
现在,当我使用返回的数据(使用InputStream
or getBytes
)创建 abitmap
并将 a 设置ImageView
为此位图setImageBitmap
时,我得到空的 imageview。即,decodeByteArray
或decodeStream
返回NULL。
我在整个互联网上搜索了这个,发现许多开发人员都面临这个问题。请尽快帮忙。
谢谢你。
我检索文本细节没有问题,检索图像是个问题。这是Java和PHP的代码片段..
这是我的 java 的 onCreate 方法。
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.loginlayout);
TextView tv1, tv2, tv3, tv4, tv5;
imageview = (ImageView) findViewById(R.id.imageView1);
button = (Button) findViewById(R.id.button1);
tv1 = (TextView) findViewById(R.id.dispuser);
tv2 = (TextView) findViewById(R.id.tv2);
tv3 = (TextView) findViewById(R.id.tv3);
tv4 = (TextView) findViewById(R.id.tv4);
tv5 = (TextView) findViewById(R.id.tv5);
SharedPreferences userDetails = this.getSharedPreferences("logindetails", MODE_PRIVATE);
String username = userDetails.getString("username", "");
String password = userDetails.getString("password", "");
button.setOnClickListener(this);
httpclient = new DefaultHttpClient();
httppost = new HttpPost("myurl");
try{
nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
if(response.getStatusLine().getStatusCode() == 200){
entity = response.getEntity();
if(entity != null) {
InputStream instream = entity.getContent();
JSONObject jsonResponse = new JSONObject(convertStreamToString(instream));
String sex = jsonResponse.getString("Sex");
String age = jsonResponse.getString("Age");
String name = jsonResponse.getString("Name");
String phone = jsonResponse.getString("Phone");
String pic = jsonResponse.getString("picture");
byte[] image = Base64.encodeBytesToBytes(pic.getBytes("UTF-8"));
imageview.setImageBitmap(BitmapFactory.decodeByteArray(image, 0, image.length));
tv1.setText("Welcome " + username);
tv2.setText("Your is Name: " + name);
tv3.setText("Your phone " + phone);
tv4.setText("Your a " + sex );
tv5.setText("Your age is " + age);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
httppost.abort();
}
}
在PHP方面..
<?
include 'connect.php';
$username = $_POST['username'];
$password = $_POST['password'];
$query = mysql_query("SELECT * FROM applogin WHERE username = '$username' AND password = '$password'");
$num = mysql_num_rows($query);
if($num == 1)
{
while($list = mysql_fetch_assoc($query))
{
$output = $list;
}
echo json_encode($output);
mysql_close();
}
?>