如何在可移动SD卡中写入任何内容

问题描述 投票:0回答:1

我想将.zip文件解压缩到可移动sd卡中。我正在使用文件选择器选择.zip文件。选择文件后,我将得到如下路径:

“ content://com.android.externalstorage.documents/document/13E7-3B0A%3Azipfolder.zip”然后选择文件夹以将文件解压缩到所选位置它给出了这样的路径“ content://com.android.externalstorage.documents/tree/13E7-3B0A%3ALOST.DIR”。之后,我使用一种方法来解压缩.zip文件System.IO.Compression.ZipFile.ExtractToDirectory(“ content://com.android.externalstorage.documents/document/13E7-3B0A%3Azipfolder.zip”,“ content://com.android.externalstorage.documents/tree/13E7-3B0A%3ALOST.DIR”);

但是它给出了下面提到的错误:

未处理的异常:System.UnauthorizedAccessException:访问路径“ / content:”被拒绝。发生

我也尝试赋予运行时权限,但仍然出现相同的错误。请帮助我,因为我在此上花了太多时间

用于选择文件并获取路径的代码

private async void SelectZipFile(object sender, EventArgs e)
        {
            string[] allowedTypes = { "application/zip" };//i use allowedType in PickFile for selecting only .zip file
            FileData file = await CrossFilePicker.Current.PickFile(allowedTypes);
            if (file != null)
            {
                //string filename = file.FileName;
                _zipFile = file.FilePath;//"content://com.android.externalstorage.documents/document/13E7-3B0A%3Azipfolder.zip"                
                txt1.Text = file.FileName;
                //int indx=_zipFile.IndexOf(filename);
                //string ss=_zipFile.Remove(indx);
                //_zipFile = ss + "/" + filename;
            }
        }

解压缩文件的代码

public static void Unzip(String _location)
        {
            if (_location != null)
            {
                Label lbl1 = new Label();
                lbl1.Text = "Extracting files.......";
                System.IO.Compression.ZipFile.ExtractToDirectory(_zipFile, _location);
                lbl1.IsVisible = false;
            }                
        }

获取位置的代码

public void SelectDirectory()
        {
            try
            {
                activity = Xamarin.Forms.Forms.Context as MainActivity;
                activity.Intent = new Intent();
                activity.Intent.SetAction(Intent.ActionOpenDocumentTree);                              
                activity.StartActivityForResult(activity.Intent, REQUEST_CODE_OPEN_DIRECTORY);
                activity.ActivityResult += (object sender, ActivityResultEventArgs e) =>
                {
                    FolderPath = e.Intent.Data;
                    //string location = FolderPath.ToString();
                    //string[] arr = FolderPath.LastPathSegment.Split(":");
                    //int indx = location.IndexOf(arr[1]);
                    //string ss = location.Remove(indx);
                    //location = ss + "/" + arr[1];
                    MainPage.Unzip(FolderPath.ToString());
                };

            }
            catch(Exception ex)
            {
                string str = ex.ToString();
            }                       
        }
xamarin xamarin.forms
1个回答
0
投票

[您可以使用我创建的库,它使这项工作变得如此简单,可以使用android存储访问框架(SAF)来支持SD卡,USB和外部存储:https://github.com/madnik7/PortableStorage

private const int browseRequestCode = 100; //Just a unique number

// Select a folder by Intent 
private void BrowseOnClick(object sender, EventArgs eventArgs)
{
     SafStorageHelper.BrowserFolder(this, browseRequestCode);
}

// Access the folder via SafStorgeProvider
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);
    if (requestCode == BROWSE_REQUEST_CODE && resultCode == Result.Ok)
    {
        var uri = SafStorageHelper.ResolveFromActivityResult(this, data);
        var storage = SafStorgeProvider.CreateStorage(this, uri);
        storage.CreateStorage("_PortableStorage.Test");
        storage.WriteAllText("test.txt", "123");
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.