2015-05-14 60 views
0

我使用Connect-SDK-Android-API-Sampler並使用mediaURL「www.example.com/image.jpg」將圖像共享到電視機中。連接SDK:共享本地圖像

如何從設備共享我的本地圖像?

+0

這似乎在被覆蓋[該SDK的FAQ。](http://connectsdk.com/docs/android/faq/) –

+0

感謝您的快速反應,我讀了常見問題並沒有看見。 我想過這個解決方案,但希望有另一個解決方案來解決我的問題 – DuoBlood

回答

0

我的問題的解決方案是Web服務器。我用了一個NanoHTTPD庫。

public class WebServer extends NanoHTTPD { 

    private int port; 

    public WebServer(int port) { 
     super(port); 
     this.port = port; 
    } 

    @Override 
    public Response serve(IHTTPSession session) { 
     Method method = session.getMethod(); 
     switch (method) { 
      case GET: 
       String path = session.getUri().replace(Utils.getIPAddress(true) + ":" + port, ""); 

       //TODO refactoring 
       String type = FilenameUtils.getExtension(path); 
       String filePath = Environment.getExternalStorageDirectory() + path; 
       String contentType = ""; 
       if (type.equals("jpg") || type.equals("jpeg") || type.equals("png") || type.equals("gif") || type.equals("tiff")) 
        contentType = "image/" + type; 
       else 
        return new Response(Response.Status.BAD_REQUEST, NanoHTTPD.MIME_PLAINTEXT, "HTTPError: HTTP 8: BAD REQUEST"); 

       if (fileInputStream != null) 
        fileInputStream.close(); 
       try { 
        fileInputStream = new FileInputStream(filePath); 
       } catch (FileNotFoundException e) { 
        new Response(Response.Status.BAD_REQUEST, NanoHTTPD.MIME_PLAINTEXT, "HTTPError: HTTP 8: BAD REQUEST"); 
       } 
       return new Response(Response.Status.OK, contentType, fileInputStream); 

      default: 
       return new Response(Response.Status.METHOD_NOT_ALLOWED, NanoHTTPD.MIME_PLAINTEXT, "HTTPError: HTTP 405: Method Not Allowed"); 
     } 
    } 
}