zoukankan      html  css  js  c++  java
  • c# 枚举安卓系统中所有目录及文件名

    using Android.App;
    using Android.Widget;
    using Android.OS;
    using System.Runtime.InteropServices;
    
    namespace App2
    {
        [Activity(Label = "App2", MainLauncher = true, Icon = "@drawable/icon")]
        public class MainActivity : Activity
        {
            [DllImport("TestDynamicSharedLib.so")]
            public extern static int getClickCount();
    
            protected override void OnCreate(Bundle bundle)
            {
                var c = getClickCount();
    
                string folderPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
    printAllSubFiles(folderPath); base.OnCreate(bundle); // Set our view from the "main" layout resource // SetContentView (Resource.Layout.Main); } void printAllSubFiles(string path) { System.Diagnostics.Debug.Print("dir:" + path); var dir = new System.IO.DirectoryInfo(path); try { foreach (var f in dir.GetFiles()) { System.Diagnostics.Debug.Print("file:" + f.FullName); } } catch { //throw; } try { foreach (var d in dir.GetDirectories()) { printAllSubFiles(d.FullName); } } catch (System.Exception) { //throw; } } } }

     如果是android 6.0 以上系统,须代码主动申请权限,此时需要Android.Support.v4。

    nuget command : Install-Package Xamarin.Android.Support.v4 -Version 28.0.0.1

    void checkPermission(string permissionName)
            {
                if (Android.Support.V4.App.ActivityCompat.CheckSelfPermission(this, permissionName)
                    != (int)Android.Content.PM.Permission.Granted)
                {
                    //用户已经拒绝过一次,再次弹出权限申请对话框需要给用户一个解释
                    if (Android.Support.V4.App.ActivityCompat.ShouldShowRequestPermissionRationale(this, permissionName))
                        Toast.MakeText(this, "请开通相关权限,否则无法正常使用本应用!", ToastLength.Short).Show();
    
                    //申请权限
                    Android.Support.V4.App.ActivityCompat.RequestPermissions(this, new String[] { permissionName }, 0);
                    Toast.MakeText(this, "请开通相关权限,否则可能无法正常使用本应用!", ToastLength.Short);
                }
                else
                {
                    Toast.MakeText(this, "授权成功!", ToastLength.Short);
                }
            }
    
    调用:
    
    checkPermission(Android.Manifest.Permission.ReadExternalStorage);
    checkPermission(Android.Manifest.Permission.WriteExternalStorage);
  • 相关阅读:
    笔记:Struts2 的 JSON 插件
    笔记:Struts2 拦截器
    笔记:Struts2 文件上传和下载
    笔记:Struts2 文件上传和下载
    【学习总结】推荐系统-协同过滤原理
    【刷题】牛客网看到的鹅厂ML面筋-部分问题RecSys相关
    【刷题】【LeetCode】000-十大经典排序算法
    【刷题】【LeetCode】总
    【问题解决方案】pygame生成的窗口点右上角关闭按钮未响应问题的解决
    【刷题】若串 =’software’ ,其子串数目为:37
  • 原文地址:https://www.cnblogs.com/nanfei/p/10299458.html
Copyright © 2011-2022 走看看