0

I'm trying to download a file (that can be either image or PDF) that I've saved in my DB. I get the file in a string format, so I want to download this file. The problem is, I don't get any error, it just doesn't show the downloaded file on its folder. Here is what I have so far.

                string caminho = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                string caminho_arquivo = Path.Combine(caminho, nome_arquivo);
                var decoded = Encoding.UTF8.GetString(Convert.FromBase64String(arquivo.arquivo));
                File.WriteAllText(caminho_arquivo, decoded);
Mario Segalla
  • 97
  • 1
  • 2
  • 9
  • "it just doesn't show the downloaded file" - how are you checking this? Which platform are you testing on? – Jason Aug 10 '20 at 17:28
  • I'm testing on my phone(AZUS zenfone 5). I get the return filepath(caminho_arquivo) and manually navigate to that folder. – Mario Segalla Aug 10 '20 at 17:30
  • Please note that there are no image formats (unless you count SVG as image) that are text and PDF is not text format also. Please clarify how file is stored so question can be answered. – Alexei Levenkov Aug 10 '20 at 17:30
  • "manually navigate" - how specifically are you doing this? You typically cannot browse to app specific folders unless your device has been rooted. You can use ADB to retrieve folders from the device and explore them. – Jason Aug 10 '20 at 17:32
  • @AlexeiLevenkov I get the image from my DB and I decode to base64 string. – Mario Segalla Aug 10 '20 at 17:35
  • @Jason I plugged my phone in the computer, I navigated to the folder there. – Mario Segalla Aug 10 '20 at 17:36
  • Are you sure you know exactly where `Environment.SpecialFolder.Personal` is located on your device? If you're on Android, the folder requires root access to view. – Collen Aug 10 '20 at 18:43

1 Answers1

0

The code below:

  string caminho = Environment.GetFolderPath(Environment.SpecialFolder.Personal);

would show the path like:

"/data/user/0/packagename/files/filename"

In Internal Storage, you couldn't see the files without root permission.

But you could use the code to check the file exist or not in the internal storage.

  if (File.Exists(caminho_arquivo))
        {

        }

If you want to view it, you could use adb tool. Please refer to the way in the link about the adb tool. How to write the username in a local txt file when login success and check on file for next login?

Wendy Zang - MSFT
  • 10,509
  • 1
  • 7
  • 17
  • I did that, the problem is that is always false. – Mario Segalla Aug 11 '20 at 12:26
  • I changed the **File.WriteAllText** to **File.WriteAllBytes** and now the file show's inside the folder. The problem now is that I can't find on my phone. – Mario Segalla Aug 11 '20 at 12:37
  • The folder you used is internal storage. The file would not show unless you have the root permission or use the adb tool. You could use the external storage instead and you could find it in your device. `string path = Android.App.Application.Context.GetExternalFilesDir(null).ToString();` – Wendy Zang - MSFT Aug 12 '20 at 06:46
  • I did what you said. Now I get permission error. I have in my manifest.xml **WRITE_EXTERNAL_STORAGE** and **READ_EXTERNAL_STORAGE** both set to true. Do I need anything else? – Mario Segalla Aug 13 '20 at 12:27
  • Before saving the file, you have to grant the permission for using the storage.Check this post [Permissions](https://learn.microsoft.com/en-us/xamarin/essentials/permissions?context=xamarin%2Fxamarin-forms&tabs=android) – Ardit Aug 14 '20 at 08:37
  • @MarioSegalla For Xamarin.Forms, you could use the Permissions Plugin. Please check the link below about the runtime permission example. https://stackoverflow.com/questions/62295556/xamarin-forms-webview-not-working-with-webrtc – Wendy Zang - MSFT Aug 14 '20 at 09:23