2013-01-25 23 views
0

我將數據發佈到來自java的URL,它使用PHP從Web服務器上的MySQL字段返回json_encode(d) blob。使用java從MySQL中未成功檢索位圖

現在,當我使用這個返回的數據(使用InputStreamgetBytes)創建bitmap並設置ImageView使用setImageBitmap此位,我得到空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(); 
    } 
    ?> 
+3

請張貼你的代碼。 – Asaph

+0

你說圖像在服務器端以'blob'存在,但是我的讀數表明'json_encode'不會發送二進制數據。在發送給客戶端之前是否忘記在服務器上進行「Base64」編碼?我也會打印有效載荷以確保它包含預期的數據。 –

+0

謝謝Altaf和David。你的幫助幫助我找出解決方案。在服務器端,我按照David的指示進行編碼,Java代碼遵循Altaf的想法。 – Vijay

回答

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