Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
92 views
in Technique[技术] by (71.8m points)

c# - How can I download a binary file to an Android device using a URL?

I am trying to download .xlsx and .pdf files from a URL into an Android device's internal memory. I am using a Unity Asset (Native Android Toolkit) but I have also tried using an alternative method. For some reason, nothing happens (i.e. file doesn't download when method is called). What am I doing wrong? (I have tried using Application.persistentDataPath as well as the internal memory path from NAT Asset)

public void StartFileDownload(string downloadLink, 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);
        return;
    }
    string memoryPath = NativeAndroid.File.GetInternalMemoryPath(); //get path of internal memory
    string newFileName = fileName.Replace(" ", "-"); //make sure no issues with name of file
    
    //download into byte array
    WebClient myWebClient = new WebClient();
    byte[] bytes = myWebClient.DownloadData(downloadLink);
    File.WriteAllBytes(memoryPath + @"/" + newFileName + extension, bytes);
    File.WriteAllBytes(Application.persistentDataPath + @"/" + newFileName + extension, bytes);

    //NativeAndroid.File.CreateFile(memoryPath + "/" + newFileName + extension, bytes); //change to any document
    NativeAndroid.Notifications.ShowToast("Download successful!", false, ToastPosition.Bottom);

}

Any help would be appreciated (even if it means using a completely different method). Thank you!

question from:https://stackoverflow.com/questions/65951332/how-can-i-download-a-binary-file-to-an-android-device-using-a-url

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...