【问题标题】:Connect SDK: share local imageConnect SDK:共享本地镜像
【发布时间】:2020-01-13 18:32:16
【问题描述】:

我使用 Connect-SDK-Android-API-Sampler 并使用 mediaURL "www.example.com/image.jpg" 将图像分享到电视。

如何从设备共享我的本地图像?

【问题讨论】:

  • 这似乎包含在the FAQ of that SDK.
  • 感谢您的快速回复,我阅读了常见问题解答并没有看到它。我考虑过那个解决方案,但希望有另一种解决方案来解决我的问题

标签: android share live-connect-sdk


【解决方案1】:

我的问题的一个解决方案是 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");
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    • 2019-11-24
    • 1970-01-01
    • 2017-01-31
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    相关资源
    最近更新 更多