未经作者许可,我无法在Android(Xamarin)中创建doc或docx文件。仅只读

时间:2018-12-17 12:16:20

标签: xamarin.android

我正在尝试复制本地文件并对其进行编辑。但是,当我复制文件时,将以只读权限创建新文件。

我尝试在其他文件夹中创建一个新文件,但是发生同样的事情。

我的代码: (复制本地文件)

//anexoPath (local file path)
//anexoPath (new file path)

System.IO.File.Copy(anexoPath, anexoPathSigned);
System.IO.File.SetAttributes(anexoPathSigned, FileAttributes.Normal);

System.IO.FileInfo fileInfo = new System.IO.FileInfo(anexoPathSigned);
fileInfo.IsReadOnly = false;

(新文件)

FileStream fs = new FileStream(anexoPathSigned,
                               FileMode.Create,
                               FileAccess.ReadWrite,
                               FileShare.ReadWrite,
                               4096,
                               FileOptions.Asynchronous);


byte[] bytes = Encoding.ASCII.GetBytes("FileStream Test");
fs.Write(bytes, 0, bytes.Length);
fs.Close();

两种情况下的结果: enter image description here

OBS:当我打开单词并在同一路径中保存新文件时,工作吧!

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我通过以下步骤解决了您的问题(您可以在github上获得完整的解决方案)

1在AndroidManifest.xml中添加权限:

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

2使用以下方法设置文件路径:

var exoFilepath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/test.txt";

3需要运行时权限检查是否将在Android 6.0(API 23级别23)或更高版本上运行的应用程序。

private void CheckWriteFliePermissions()
    {
        // Check if the Camera permission is already available.
        if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) != (int)Permission.Granted
            || ActivityCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) != (int)Permission.Granted)
        {

            // Write permission has not been granted
            RequestWriteFilePermissions();

        }
        else
        {
            // Wrie File permissions is already available
            Log.Info("MainActicity", "Write file permission has already been granted.");
            ProcessFiles();
        }
    }

    void RequestWriteFilePermissions()
    {
        Log.Info("MainActicity", "Write file permission has NOT been granted. Requesting permission.");

        if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.ReadExternalStorage)
            || 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 user has previously denied the permission.
            Log.Info("MainActicity", "Displaying Write file permission rationale to provide additional context.");

            Snackbar.Make(layout, "Write file permission is needed to write files.",
                Snackbar.LengthIndefinite).SetAction("OK", new Action<View>(delegate (View obj) {
                    ActivityCompat.RequestPermissions(this, PERMISSIONS_READWRITE, 0);
                })).Show();
        }
        else
        {
            // Camera permission has not been granted yet. Request it directly.
            ActivityCompat.RequestPermissions(this, PERMISSIONS_READWRITE, 0);
        }
    }

    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
    {
        if (requestCode == 0)
        {
            // Received permission result for camera permission.
            Log.Info("MainActivity", "Received response for write file permission request.");

            // Check if the only required permission has been granted
            if (PermissionUtil.VerifyPermissions(grantResults))
            {
                // All required permissions have been granted, display contacts fragment.
                ProcessFiles();
            }
            else
            {
                Log.Info("MainActivity", "Contacts permissions were NOT granted.");
            }
        }

        else
        {
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    public void ProcessFiles()
    {

        var exoFilepath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/test.txt";
        Log.Debug("MainActivity", "exoFilepath"+ exoFilepath);
        FileStream fs = new FileStream(exoFilepath,
                           FileMode.Create,
                           FileAccess.ReadWrite,
                           FileShare.ReadWrite,
                           4096,
                           FileOptions.Asynchronous);


        byte[] bytes = Encoding.ASCII.GetBytes("FileStream Test");
        fs.Write(bytes, 0, bytes.Length);
        fs.Close();

    }