Does this code look like the right way to create a file from an InputStream??
Thanks
B4X:
/**
* Gets a File from an Asset
*
*/
public void GetFileFromAsset(Asset asset, final String TargetDir, final String TargetFilename) {
if (asset == null) {
throw new IllegalArgumentException("Asset cannot be null");
}
PendingResult<DataApi.GetFdForAssetResult> pendingResult = Wearable.DataApi.getFdForAsset(mGoogleApiClient, asset);
pendingResult.setResultCallback(new ResultCallback<DataApi.GetFdForAssetResult>() {
@Override
public void onResult(DataApi.GetFdForAssetResult result) {
try {
InputStream assetInputStream = result.getInputStream();
OutputStream assetOutputStream = new FileOutputStream(File.Combine(TargetDir, TargetFilename));
byte[] buffer = new byte[1024];
int bytesRead = 0;
while((bytesRead = assetInputStream.read(buffer)) != -1) {
assetOutputStream.write(buffer, 0, bytesRead);
}
assetInputStream.close();
assetOutputStream.flush();
assetOutputStream.close();
} catch (IOException e) {
BA.Log(e.toString());
}
}
});
}
Thanks