关闭前删除! c #android

时间:2017-09-27 02:38:53

标签: c# android xamarin delete-file

我想删除内部智能手机存储设备中相机文件夹中的所有文件!

我该怎么办?

    namespace App1
{
    [Activity(Label = "App1", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);        
           SetContentView (Resource.Layout.Main);

            Button delete = FindViewById<Button>(Resource.Id.deleteid);

             delete.Click += delegate
             {
                 File dir = new File(Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDcim), "/Camera");

                 if (dir.IsDirectory)
                  {
                       int i = 0;
                       string[] children = dir.List();
                          while ( i < children.Length)
                       {
                        var file = new File(dir, children[i]).Delete();


                           i++;
                       }

                      Toast.MakeText(this, "The folder exist", ToastLength.Short).Show();
                  }

                  else
                  {
                      Toast.MakeText(this, "Not Works", ToastLength.Short).Show();
                  }
             };
        }
    }

}

我尝试使用此代码删除文件夹的内容,但应用程序崩溃了。

1 个答案:

答案 0 :(得分:0)

您需要创建接收器以侦听关闭事件。为此,您应该实现类Service,并且不要在清单文件中添加以下行:

<action android:name="android.intent.action.ACTION_SHUTDOWN" />
<action android:name="android.intent.action.QUICKBOOT_POWEROFF" />

此外,对于删除用户数据,请确保您有权执行此操作:

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