android 使用Java从MySQL检索位图失败

yftpprvb  于 2023-04-10  发布在  Android
关注(0)|答案(1)|浏览(106)

我将数据从Java发送到一个URL,Java使用PHP从Web服务器上的MySQL字段返回一个json_encode(d) blob。
现在,当我使用这个返回的数据(使用InputStreamgetBytes)来创建一个bitmap,并使用setImageBitmapImageView设置为这个位图时,我得到空的imageview。即,decodeByteArraydecodeStream返回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();
    }
    ?>
14ifxucb

14ifxucb1#

String input;//your pic string 
byte[] decodedByte = Base64.decode(input, 0);
BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);

相关问题