2012-04-17 41 views
-1

我工作的ImageView中,我想設置的圖像轉換成的ImageView但圖像(字節數組)設置圖像直接從Web服務器來..這個概念是:直接從Web服務器來的ImageView

1)我向Web服務器發送各種參數,並且作爲回報,Web服務器向我發送一個特定ID的圖像(只是一個圖像,而不是URL)

2)現在我想直接將Image ImageView而不保存到SD卡中,以便每當用戶想要查看任何其他ID相關的圖像時,圖像應該直接來自服務器,並且我們不會將其保存在我們的末端。

[Editted]從服務器

取值並返回它在字節[]

 static BufferedReader in=null; 

byte[] result_image; 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 

     HttpPost httpPost = new HttpPost(uri1); 
     if(list!=null) 
     { 

     UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(list); 
     httpPost.setEntity(formEntity); 

     } 
     //URI uri=httpPost.getURI(); 
     HttpResponse httpResponse = httpClient.execute(httpPost); 
     in = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent())); 
     StringBuffer sb = new StringBuffer(""); 
     String line = ""; 
     // String NL = System.getProperty("line.separator"); 
     while ((line = in.readLine()) != null) { 
       sb.append(line +" "); //sb.append(line +NL); 
     } 
     in.close(); 



     result_image = String.valueOf(sb).getBytes(); 

} 
    catch(UnsupportedEncodingException e) 
    { 
     String err = (e.getMessage()==null)?"Cant connect to server":e.getMessage(); 
     Log.e("Network Error:",err); 
    } 
    catch (MalformedURLException e) { 
     String err = (e.getMessage()==null)?"Malformed Exception":e.getMessage(); 
     Log.e("Malformed Exception:",err); 

    } 
    catch(Exception ex) 
    { 
     // Log.i("Exception,ex", ex.getMessage()); 
     String err = (ex.getMessage()==null)?"NetworkConnectionException":ex.getMessage(); 
     Log.e("NetworkConnectionException:",err); 
    } 
    finally { 

     if (in != null) { 
      try { 
        in.close(); 
      } catch (Exception ex) { 
       String err = (ex.getMessage()==null)?"Excepion":ex.getMessage(); 
       Log.e("Exception:",err); 
      } 
     } 

這裏result_image是一個字節[]和使用該字節[]正在對其進行解碼並在位圖shwing它..請幫助... 任何幫助,這是非常感謝.. 謝謝..

+3

Web服務器發送給我一張圖片?在哪種格式?是base64字符串,字節數組還是文件? – user370305 2012-04-17 06:23:42

+0

它是一個字節數組 – Kanika 2012-04-17 06:24:57

回答

0

decodeByteArray()方法是建議的方式做到這一點。 如果它不適合你,你可以像下面的代碼手動解碼它。

URL yourl = new URL(src); 
HttpURLConnection connection = (HttpURLConnection) yourl.openConnection(); 
connection.setDoInput(true); 
connection.connect(); 
InputStream input = connection.getInputStream();         
bis = new BufferedInputStream(input,int);     
baf = new ByteArrayBuffer(int); 

int current = 0;       
while ((current = bis.read()) != -1) {     
baf.append((byte) current); 
} 

希望上面的代碼向您展示一些替代想法。快樂的編碼。

0

試試這個:

Bitmap bMap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); 
image.setImageBitmap(bMap); 
3

你可以使用這樣

URL url = new URL(src); 
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
connection.setDoInput(true); 
connection.connect(); 
InputStream input = connection.getInputStream(); 
Bitmap myBitmap = BitmapFactory.decodeStream(input); 
imageView.setImageBitmap(myBitmap); 
0

首先,你可以不設置網址的ImageView的數據源。您必須從URL獲得響應並將其轉換爲字節。

其次,如果您不想將圖像保存到SDCARD上,則只需在ImageView中指定字節數組,然後在此處將圖像保存到SDCARD中。字節數組將保留直到您的應用程序在RAM中處於活動狀態。

代碼如下。這裏URL指向圖像像 http://domain/test.jpg

URL myFileUrl =null;   
     try { 
      myFileUrl= new URL(url); 
     } catch (MalformedURLException e) { 
      return false; 
     } 
     try { 
      HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection(); 
      conn.setInstanceFollowRedirects(false); 
      conn.setDoInput(true); 
      conn.connect();      
      InputStream is = conn.getInputStream(); 
      Bitmap bitmap = BitmapFactory.decodeStream(is); 
      image.setImageBitmap(bitmap); 
      return true; 
     } catch (IOException e) { 
      System.out.println("error="+e.getMessage()); 
      e.printStackTrace();   
      return false; 
     } 
0
if we use asynctask then like this 
//declare Bitmap 
//new Loading().execute("http://Hsot/123.jpg"); //Call the asynctask 

protected Bitmap doInBackground(String... args) 
{   
bitmap=BitmapFactory.decodeStream((InputStream)newURL(args[0]).getContent()); 
return bitmap; 
} 
protected void onPostExecute(Bitmap image) 
{ 
    my_image.setImageBitmap(image); 
    my_image2.setImageBitmap(image); 
}