2016-07-29 22 views
0

Wenn ich den Web-Service in Android anrufe, ruft es das Bild ab, aber als das Bild ist Stream Objekt Glide nicht verarbeiten.Parse Bild, das ich vom Web-Service empfange. Es ist ein Stream-Objekt. Wie kann ich das in Android abrufen?

Hier ist mein Code des Web Service.

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Web; 
using System.Text; 

namespace ImageTransferService 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together. 
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging. 
    public class Service1 : IService1 
    { 
     public Stream GetImage(int i) 
     { 
      if (i == 5) 
      { 
       FileStream imageFilePath = File.OpenRead(@"E:\Images\h1.jpg"); 
       WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg"; 
       return imageFilePath; 

      } 
      else 
       return null; 

     } 


    } 
} 

Dienst

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Web; 
using System.Text; 

namespace ImageTransferService 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together. 
    [ServiceContract] 
    public interface IService1 
    { 
     [OperationContract] 
     [WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] 

     Stream GetImage(int i); 

    } 
    } 

Meine Android Code:

package com.example.unais.myapplication; 

import android.util.Log; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.StringEntity; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONObject; 
import org.json.JSONStringer; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException; 

/** 
* Created by Unais on 7/26/2016. 
*/ 
public class WebService { 

    private final static String URL = "http://192.168.1.21:1313/Service1.svc/"; 
    private static final String REGISTER_METHOD = "GetImage"; 


    public InputStream CallService(String methodName, JSONStringer json) { 

     try { 


      HttpPost request = new HttpPost(URL + methodName); 
      request.setHeader("Accept", "application/json"); 
      request.setHeader("Content-type", "application/json"); 


      StringEntity entity = new StringEntity(json.toString()); 
      request.setEntity(entity); 

      // Send request to WCF service 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpResponse response = httpClient.execute(request); 

      HttpEntity responseEntity = response.getEntity(); 
      InputStream stream = responseEntity.getContent(); 
      return stream; 

//   // Read response data into buffer 
//   BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "iso-8859-1"), 8); 
// 
//   StringBuilder sb = new StringBuilder(); 
//   String line = null; 
//   while ((line = reader.readLine()) != null) { 
//    sb.append(line + "\n"); 
//   } 
//   stream.close(); 

//   Log.d("asgasgasgasgaga", sb.toString()); 
//   return sb.toString(); 

     } catch (UnsupportedEncodingException e) { 
      Log.d("Exception", "Exception"); 
      e.printStackTrace(); 
     } catch (IOException e) { 
      Log.d("Exception", "Exception"); 
      e.printStackTrace(); 
     } 

     return null; 
    } 



    public InputStream GetImages() 
    { 
     Log.d("Function","In Function Get Images"); 
     try 
     { 
      JSONStringer registerJson = new JSONStringer() 
        .object() 
        .key("i").value(5) 
        .endObject(); 

      return (CallService(REGISTER_METHOD, registerJson)); 
     } catch (Exception ex) 
     { 
      ex.printStackTrace(); 
     } 
     return null; 
    } 
    } 
@Override 
     public void onBindViewHolder(MyViewHolder holder, int position) 
     { 

      Image image = images.get(position); 



      Glide.with(mContext).load(image.getIota()) 
        .thumbnail(0.5f) 
        .crossFade() 
        .diskCacheStrategy(DiskCacheStrategy.ALL) 
        .into(holder.thumbnail); 
     } 

Wenn Sie einen anderen Code möchte ich kann provide.I auf diese stucked bin so bitte helfen Sie mir:/

Antwort

0

Ihr Problem m ist Load Image dieses Beispiel Last Bild sehen, in

Uri.parse (xxx)

String assetPath = "file:///android_asset/flags/FR.jpg"; 
Glide.with(getContext()) 
      .load(Uri.parse(assetPath)) 
      .placeholder(missingFlagDrawable) 
      .centerCrop() 
      .crossFade() 
      .into(flag); 

Siehe diesen Link Load image in Glide