[英]Send Base64 encoded image as string to Chromecast
The problem is to send local image from phone as encoded Base64 string to Chromecast. 问题是将电话中的本地图像作为编码的Base64字符串发送到Chromecast。 And decode it using my Custom Receiver.
并使用我的自定义接收器对其进行解码。 I was following this guide which is based on this project sample .
我正在遵循基于该项目样本的 本指南。
I suggest the problem might be in: 我建议问题可能出在:
You see, it seems like I coded everithing right since the status of Chromecast when I send photo is: 您会发现,由于我发送照片时Chromecast的状态为:
statusCode 0 (success),
application name: Default Media Receiver,
status: Ready To Cast,
sessionId: 34D6CE75-4798-4294-BF45-2F4701CE4782,
wasLaunched: true.
This is how I send image as String: 这就是我将图像作为String发送的方式:
mCastManager.castImage(mCastManager.getEncodedImage(currentEntryPictureByPoint.getPath()));
Methods used: 使用的方法:
public void castImage(String encodedImage)
{
Log.d(TAG, "castImage()");
String image_string = createJsonMessage(MessageType.image, encodedImage);
sendMessage(image_string);
}
private static String createJsonMessage(MessageType type, String message)
{
return String.format("{\"type\":\"%s\", \"data\":\"%s\"}", type.toString(), message);
}
/**
* Convert Image to encoded String
* */
public String getEncodedImage(String path){
Bitmap bm = BitmapFactory.decodeFile(path);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] byteArrayImage = baos.toByteArray();
String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
return encodedImage;
}
/**
* Send a text message to the receiver
*
* @param message
*/
private void sendMessage(String message) {
if (mApiClient != null && mCustomImageChannel != null) {
try {
Cast.CastApi.sendMessage(mApiClient,
mCustomImageChannel.getNamespace(), message)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status result) {
if (!result.isSuccess()) {
//ALWAYS REACHING HERE :(
Log.e(TAG, "Sending message failed");
}
}
});
} catch (Exception e) {
Log.e(TAG, "Exception while sending message", e);
}
} else {
Toast.makeText(mContext, message, Toast.LENGTH_SHORT)
.show();
}
}
If the sending process is correct then the Receiver is wrong and don't know how to decode this message properly. 如果发送过程正确,那么接收方是错误的,并且不知道如何正确解码此消息。 The way I uploaded it (well, at least I think that its uploaded...)
我上传它的方式(嗯,至少我认为它上传了...)
urn:x-cast:com.it.innovations.smartbus
urn:x-cast:com.it.innovations.smartbus
If anyone faced this problem, please point me out what I am doing wrong. 如果有人遇到此问题,请指出我做错了什么。 Appreciate any help.
感谢任何帮助。
PS tell if some more code needed... PS告诉是否需要更多代码...
I very strongly suggest to avoid using the sendMessage()
for sending any large data set; 我强烈建议避免使用
sendMessage()
发送任何大数据集。 those channels are meant to be used as control channels and not as a way to send a chunk of data. 这些通道应被用作控制通道,而不是作为发送数据块的方式。 A much much simpler and more robust approach is to embed a tiny dumb web server in your local app (on the sender side) and "serve" your images to your chromecast.
一种更简单,更可靠的方法是在本地应用程序中(发送方)嵌入一个小型的Web服务器,然后将图像“提供”给chromecast。 There is a number of ready-to-use embedded web servers that you can put in your app and requires almost no configuration;
您可以在应用程序中放入许多现成的嵌入式Web服务器,并且几乎不需要进行任何配置。 then you can serve all sorts of media, including images, to your chromecast with even the default or styled receiver.
那么您甚至可以使用默认或样式化的接收器将各种媒体(包括图像)投放到chromecast。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.