Как записать byte[] в файл и запустить установку
У меня есть отправка файла с телефона а точнее файл конвертируется в byte и эти byte отправляются на часы.
public void onSend(View view) {
File file = new File("wearOs.apk");
executorService.execute(new Runnable() {
@Override
public void run() {
Collection<String> nodes = getNodes();
LOGD(TAG, "Nodes: " + nodes.size());
for (String node : nodes) {
Task<ChannelClient.Channel> channelTask = Wearable.getChannelClient(getApplicationContext()).openChannel(node, CHANNEL_MSG);
channelTask.addOnSuccessListener(new OnSuccessListener<ChannelClient.Channel>() {
@Override
public void onSuccess(ChannelClient.Channel channel) {
LOGD(TAG, "onSuccess " + channel.getNodeId());
Task<OutputStream> outputStreamTask = Wearable.getChannelClient(getApplicationContext()).getOutputStream(channel);
outputStreamTask.addOnSuccessListener(new OnSuccessListener<OutputStream>() {
@Override
public void onSuccess(OutputStream outputStream) {
LOGD(TAG, "output stream onSuccess");
try {
InputStream inputStream = getContentResolver().openInputStream(Uri.fromFile(file));
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) > 0)
byteBuffer.write(buffer, 0, len);
outputStream.write(byteBuffer.toByteArray());
outputStream.flush();
outputStream.close();
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
});
}
}
});
}
Не могу разобраться как мне на часах получить эти byte, записать их в файл с тем же названием и расширением и запустить установку apk файла?
Пробовал таким образом но нечего не получается
try {
File file = new File("wearOs.apk");
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int read;
byte[] data = new byte[1024];
while ((read = inputStream.read(data, 0, data.length)) != -1) {
Log.d(TAG, "data length " + read); /** use Log.e to force print, if Log.d does not work */
buffer.write(data, 0, read);
buffer.flush();
byte[] byteArray = buffer.toByteArray();
try (FileOutputStream fileOuputStream = new FileOutputStream("wearOs.apk")){
fileOuputStream.write(byteArray);
}
}
Log.d(TAG, "reading: " + file);
final File finalFile = file;
runOnUiThread(new Runnable() {
@Override
public void run() {
new Intent(Intent.ACTION_INSTALL_PACKAGE).putExtra("wearOs.apk", finalFile);
}
});
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
Wearable.getChannelClient(getApplicationContext()).close(channel);
}