2014-02-20 3 views
2

http 응답에서 이미지를 가져 오려고하지만 스트림을 비트 맵으로 변환하지 못했습니다. 알려주십시오. 여기에 무엇이 없습니까?Android의 httpResponse에서 이미지 콘텐츠 가져 오기

참고 - 이미지 콘텐츠는 원시 이진 & 이미지와 jpeg 이미지로 수신됩니다.

절차는 다음 :

  1. 만들기 HttpRequest에 있습니다.
  2. 응답에서 200 -> http 내용을 확인하십시오.
  3. BitMap 팩토리를 사용하여 스트림을 비트 맵으로 변환합니다.
  4. 사전에 AsyncTask를

    if (null != instream) { 
         Bitmap bm = BitmapFactory.decodeStream(instream); 
         if(null == bm){ 
        Toast toast = Toast.makeText(getApplicationContext(), 
         "Bitmap is NULL", Toast.LENGTH_SHORT); 
          toast.show(); 
        } 
         ImageView view = (ImageView) findViewById(R.id.picture_frame); 
        view.setImageBitmap(bm); 
        } 
    

    감사의 postExecute이를하고 AsyncTask를

    HttpClient httpclient = new DefaultHttpClient(); 
        HttpGet httpget = new HttpGet(endpoint); 
        // Adding Headers .. 
        // Execute the request 
        HttpResponse response; 
        try { 
         response = httpclient.execute(httpget); 
        if (response.getStatusLine().getStatusCode() == 200) { 
         // Get hold of the response entity 
         HttpEntity entity = response.getEntity(); 
         if (entity != null) { 
         InputStream instream = entity.getContent(); 
         return instream; 
         // instream.close(); 
          } 
        } 
    } 
    

    의 postExecute에 이렇게

를 이미지 뷰에 비트 맵을 설정합니다.

답변

4

마지막으로 이에 대한 답변이 나와 있습니다. 아래는 스 니펫입니다. http 응답으로 작업하는 새로운 사람들에게 도움이 될 수 있습니다.

HttpClient httpclient = new DefaultHttpClient(); 
HttpGet httpget = new HttpGet(endpoint); 
// Adding Headers .. 
// Execute the request 
HttpResponse response; 
try { 
    response = httpclient.execute(httpget); 
if (response.getStatusLine().getStatusCode() == 200) { 
    // Get hold of the response entity 
    HttpEntity entity = response.getEntity(); 
    if (entity != null) { 
    InputStream instream = entity.getContent(); 
    String path = "/storage/emulated/0/YOURAPPFOLDER/FILENAME.EXTENSION"; 
    FileOutputStream output = new FileOutputStream(path); 
    int bufferSize = 1024; 
    byte[] buffer = new byte[bufferSize]; 
    int len = 0; 
    while ((len = instream.read(buffer)) != -1) { 
     output.write(buffer, 0, len); 
    } 
    output.close(); 
} 

파일을 디스크에 저장하는 대신 bytearray의 내용을 가져 와서 비트 맵을 얻을 수 있습니다. 참고로

ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
int bufferSize = 1024; 
byte[] buffer = new byte[bufferSize]; 
int len = 0; 
try { 
    // instream is content got from httpentity.getContent() 
    while ((len = instream.read(buffer)) != -1) { 
    baos.write(buffer, 0, len); 
    } 
    baos.close(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
byte[] b = baos.toByteArray(); 
Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length); 
ImageView imageView = (ImageView)findViewById(R.id.picture_frame); 
imageView.setImageBitmap(bmp); 

-이 로컬 디스크에 안드로이드 fileoutput 스트림 서면으로 비 UI 스레드에서 수행되어야한다 (일부는 여기에 추가되지 않습니다 내 경우 &에서 비동기 작업을 사용했다).

감사합니다.

1

웹에서 이미지 작업을 할 때이 라이브러리를 사용하십시오. https://github.com/nostra13/Android-Universal-Image-Loader 그것은 모든 것을 해줍니다. 그러나 onPostExecute의 코드는 onDoInBackground에 넣어야합니다. onPre 및 onPost 실행 코드는 주 스레드에서 실행되고 doInBackground는 작업자 스레드입니다. 하지만이 경우 유니버설 이미지 로더 만 사용하십시오.

+0

감사합니다. 다른 라이브러리를 사용하고 싶지 않습니다. 단일 라이브러리에서 처리되는 모든 요청 및 응답을 수행하기위한 것입니다. – Dinesh