I would suggest rather using a UnityWebRequest.Get
and then do e.g.
public void StartFileDownload(string downloadLink, string fileName, string extension)
{
StartCoroutine(GetRequest(downloadLink, fileName, extension));
}
IEnumerator GetRequest(string uri, string fileName, string extension)
{
//NativeAndroid.Notifications.ShowToast(Application.persistentDataPath, false, ToastPosition.Top);
//check if there is memory in device
if (NativeAndroid.File.isInternalMemoryAvailable() == false)
{
NativeAndroid.Notifications.ShowToast("No memory available on device", false, ToastPosition.Bottom);
yield break;
}
//get path of internal memory
string memoryPath = NativeAndroid.File.GetInternalMemoryPath();
//make sure no issues with name of file
string newFileName = fileName.Replace(" ", "-");
using (var webRequest = UnityWebRequest.Get(downloadLink))
{
// Request and wait for the desired page.
yield return webRequest.SendWebRequest();
if (webRequest.isNetworkError)
{
NativeAndroid.Notifications.ShowToast($"Download failed: {webRequest.error}", false, ToastPosition.Bottom);
}
else
{
var bytes = webRequest.downloadHandler.data;
var memoryFilePath = Path.Combine(memoryPath, newFileName + extension);
File.WriteAllBytes(memoryFilePath, bytes);
var persistentFilePath = Path.Combine(Application.persistentDataPath, newFileName + extension);
File.WriteAllBytes(persistentFilePath, bytes);
//NativeAndroid.File.CreateFile(memoryFilePath , bytes);
NativeAndroid.Notifications.ShowToast("Download successful!", false, ToastPosition.Bottom);
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…