0

I'm trying to save files in the Downloads folder, I'm using this code

public void ExportData(List<Records> results)
{
    // Verify that all required contact permissions have been granted.
    if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) != (int)Permission.Granted)
    {
        // Contacts permissions have not been granted.
        RequestWriteExternalStoragePermissions();
        ExportData(results);
    }
    else
    {
        // Contact permissions have been granted. Show the contacts fragment.
        try
        {
            var internalPath = GetExternalFilesDir(Android.OS.Environment.DirectoryDownloads).AbsolutePath; //System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
            var reportsForXML = ConvertRecordsForExport(results);
            var filePath = System.IO.Path.Combine(internalPath,
                GetString(Resource.String.Statistics_ExportAllExportPrefix, firstResult.ToString("MMddyyyy"), DateTime.Today.ToString("MMddyyyy")));
            if (!System.IO.File.Exists(filePath))
            {
                using (System.IO.StreamWriter writer = new System.IO.StreamWriter(filePath, true))
                {
                    System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(reportsForXML.GetType());
                    x.Serialize(writer, reportsForXML);
                    var Sbar = Snackbar.Make(layout, GetString(Resource.String.Statistics_ExportAllExportCompleted, filePath),
                        Snackbar.LengthIndefinite).SetAction(Android.Resource.String.Ok, new Action<View>(delegate (View obj)
                        {
                        }));
                    //View snackbarView = Sbar.View;
                    TextView snackbarTextView = (TextView)Sbar.View.FindViewById(Resource.Id.snackbar_text);
                    snackbarTextView.SetMaxLines(5);
                    Sbar.Show();
                }
            }
            else
            {
                var Sbar = Snackbar.Make(layout, GetString(Resource.String.Statistics_ExportAllFileExist, internalPath),
                    Snackbar.LengthIndefinite).SetAction(Android.Resource.String.Ok, new Action<View>(delegate (View obj)
                    {
                    }));
                TextView snackbarTextView = (TextView)Sbar.View.FindViewById(Resource.Id.snackbar_text);
                snackbarTextView.SetMaxLines(5);
                Sbar.Show();
            }

        }
        catch (Exception ex)
        {
            Toast.MakeText(this, ex.Message, ToastLength.Long).Show();
            Toast.MakeText(this, GetString(Resource.String.Statistics_ExportAllError), ToastLength.Long).Show();

        }
    }
}

/**
* Requests the Contacts permissions.
* If the permission has been denied previously, a SnackBar will prompt the user to grant the
* permission, otherwise it is requested directly.
*/
void RequestWriteExternalStoragePermissions()
{
    try
    {
        if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.WriteExternalStorage))
        {

            // Provide an additional rationale to the user if the permission was not granted
            // and the user would benefit from additional context for the use of the permission.
            // For example, if the request has been denied previously.

            // Display a SnackBar with an explanation and a button to trigger the request.
            Snackbar.Make(layout, GetString(Resource.String.Statistics_ExportAllRequestPermission),
                Snackbar.LengthIndefinite).SetAction(Android.Resource.String.Ok, new Action<View>(delegate (View obj)
                {
                    ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.WriteExternalStorage }, REQUEST_WRITE_EXTERNAL);
                })).Show();
        }
        else
        {
            // Contact permissions have not been granted yet. Request them directly.
            ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.WriteExternalStorage }, REQUEST_WRITE_EXTERNAL);
        }
    }
    catch (Exception ex)
    {
        Toast.MakeText(this, ex.Message, ToastLength.Long).Show();
        Toast.MakeText(this, GetString(Resource.String.Statistics_ExportAllErrorGettingPerminion), ToastLength.Long).Show();
    }
}

But instead getting the Downloads path emulated/0/Download

I'm getting the app Downloads path emulated/0/Android/Data/com.appname.com/Downloads

Cheesebaron
  • 24,131
  • 15
  • 66
  • 118
CDrosos
  • 2,418
  • 4
  • 26
  • 49
  • check https://stackoverflow.com/questions/36695734/how-to-create-a-folder-in-download-directory-android-studio/36695883 or https://stackoverflow.com/questions/16773348/set-custom-folder-android-download-manager – Kirguduck Mar 03 '20 at 14:28
  • im using their approach also but im not getting the correct path – CDrosos Mar 03 '20 at 14:42
  • `im getting the app Downloads path `. Of course. You are asking for it. You use: ` GetExternalFilesDir()`. – blackapps Mar 03 '20 at 15:39
  • and how i can get the android Downloads folder then and not the app Downloads folder? – CDrosos Mar 03 '20 at 15:40
  • 1
    use the DownloadManager API. I don't believe the system download folder is directly accesible – Jason Mar 03 '20 at 17:05
  • yes i think the proper solutions is in this answer https://stackoverflow.com/questions/56904485/how-to-save-an-image-in-android-q-using-mediastore/56990305#56990305 although it feels like a workaround to me. so maybe i should avoid it – CDrosos Mar 03 '20 at 19:18

0 Answers0