Получить общий путь к папке загрузок от Android - PullRequest
0 голосов
/ 03 марта 2020

Я пытаюсь сохранить файлы в папке «Загрузки», я использую этот код

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

Но вместо этого я получаю путь к загрузке emulated/0/Download

Я получаю путь загрузки приложения emulated/0/Android/Data/com.appname.com/Downloads

...