Android File.listFiles()返回null问题

File.listFiles()返回null可能原因是没读取sdcard权限(READ_EXTERNAL_STORAGE WRITE_EXTERNAL_STORAGE)。

system/private-app默认都有权限,system/app某些系统模式下初始化后没权限,系统重启后有权限,第三方app的权限大部分是询问。

使用ContextCompat.checkSelfPermission检查是否有权限,使用ActivityCompat.requestPermissions申请权限。

public class MainActivity extends AppCompatActivity {private final static String PATH = Environment.getExternalStorageDirectory().getPath();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);if (isStoragePermissionGranted()) {File directory = new File(PATH);File[] files = directory.listFiles();Log.i("juno", PATH + "files : " + (files == null ? null : files.length));}}public boolean isStoragePermissionGranted() {if (Build.VERSION.SDK_INT >= 23) {final Context context = getApplicationContext();int readPermissionCheck = ContextCompat.checkSelfPermission(context,Manifest.permission.READ_EXTERNAL_STORAGE);int writePermissionCheck = ContextCompat.checkSelfPermission(context,Manifest.permission.WRITE_EXTERNAL_STORAGE);if (readPermissionCheck == PackageManager.PERMISSION_GRANTED && writePermissionCheck == PackageManager.PERMISSION_GRANTED) {Log.v("juno", "Permission is granted");return true;} else {Log.v("juno", "Permission is revoked");ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);return false;}} else { //permission is automatically granted on sdk<23 upon installationLog.v("juno", "Permission is granted");return true;}}@Overridepublic void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {super.onRequestPermissionsResult(requestCode, permissions, grantResults);Log.v("juno","onRequestPermissionsResult requestCode : " + requestCode+ " Permission: " + permissions[0] + " was " + grantResults[0]+ " Permission: " + permissions[1] + " was " + grantResults[1]);if(grantResults[0]== PackageManager.PERMISSION_GRANTED){//resume tasks needing this permissionFile directory = new File(PATH);File[] files = directory.listFiles();Log.i("juno", "After PERMISSION_GRANTED files : " + (files == null ? null : files.length));}}
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/430281.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

android中实现简单的播放

1 MediaPlayer mediaPlayer1; 2 mediaPlayer1 MediaPlayer.create(getBaseContext(), R.raw.ic_yanyuan); 3 mediaPlayer1.start(); 需要在res下面新建个raw&#xff0c;然后拖拽进去一个音乐文件。转载于:https://www.cnblogs.com/yi-mi-yangguang/p/5765401.html

python能代替vba吗_VBA会被Python代替吗?

最近内测版Excel中的vlookup函数被xlookup函数代替了&#xff0c;但vlookup函数依然得到了保留。类似的&#xff0c;对于生产力工具Office套件而言&#xff0c;保证兼容性是重要的&#xff0c;Office诞生以来出现的所有功能在最新版中依然会找得到&#xff0c;因此作为内建在Of…

获取apk安装包sha1的值

1. 解压apk&#xff0c;进入解压目录&#xff0c;找到META-INF/CERT.RSA&#xff0c; 2.执行命令 keytool -printcert -file .../META-INF/CERT.RSA 可以得到md5和sha1值 证书指纹: MD5: 8D:DB:34:2F:2D:A5:40:84:02:D7:56:8A:F2:1E:29:F9 SHA1: 27:19:6E:38:6B:…

nginx 不带www到www域名的重定向

如果是单次重定向用 redirect, 如果永久跳转用 permanent&#xff0c;这里用 permanent { listen 80; server_name xxx.com www.xxx.com; index index.html index.php; root /data/www/wwwroot; if ($http_host !~ "^www.xxx.com$") { rewrite ^(.*) ht…

python dropna失败_使用Python部署机器学习模型的10个实践经验

作者&#xff1a;Pratik Bhavsar编译&#xff1a;ronghuaiyang导读使用python部署ML项目的一些经验。有时候&#xff0c;作为数据科学家&#xff0c;我们会忘记公司付钱让我们干什么。我们首先是开发人员&#xff0c;然后是研究人员&#xff0c;然后可能是数学家。我们的首要责…

ClassCastException:AdaptiveIconDrawable cannot be cast to BitmapDrawable

Caused by: java.lang.ClassCastException: android.graphics.drawable.AdaptiveIconDrawable cannot be cast to android.graphics.drawable.BitmapDrawable 出现原因是因为应用适配了API 26的自适配图标adaptive-icon&#xff0c;使用下面的方法读取icon&#xff0c;读取到应…

【JDK1.8】Java HashMap实现细节

底层是用数组实现的 /*** The table, initialized on first use, and resized as* necessary. When allocated, length is always a power of two.* (We also tolerate length zero in some operations to allow* bootstrapping mechanics that are currently not needed.)*/tr…

Android实现圆角照片和圆形照片

方法1: 使用RoundedBitmapDrawable public static RoundedBitmapDrawable bitmapToRoundedDrawable(NonNull Resources res, NonNull Bitmap bitmap,boolean circular, float cornerRadius) {RoundedBitmapDrawable drawable RoundedBitmapDrawableFactory.create(res, bitma…

python判断字符串长度_Python|判断字符串是否符合日期要求

问题描述 1.题目要求&#xff1a; 输入一串字符,由字母、数字和空格组成&#xff0c;长度<1000,判断其中是否存在日期格式的数据。日期格式的数据具有如下的特征&#xff0c;连续包含年份和月份信息。年份信息是指连续的四个数字,之后是Jan, Feb, Mar,Apr, May, Jun, Jul, A…

剑客决斗(NYOJ 110)

T3、题目110剑客决斗 &#xff08;http://acm.nyist.net/JudgeOnline/problem.php?pid110&#xff09; 描述 在路易十三和红衣主教黎塞留当权的时代&#xff0c;发生了一场决斗。n个人站成一个圈&#xff0c;依次抽签。抽中的人和他右边的人决斗&#xff0c;负者出圈。这场决斗…

Android P 网络报错 : java.io.IOException: Cleartext HTTP traffic to ***.com not permitted

在Android P 使用HttpUrlConnection进行http请求会出现以下异常 java.io.IOException: Cleartext HTTP traffic to ***.com not permitted at com.android.okhttp.HttpHandler$CleartextURLFilter.checkURLPermitted(HttpHandler.java:115) at com.android.ok…

linux免密登录_Linux SSH免密钥登录总结

Linux下生成密钥通过命令”ssh-keygen -t rsa“生成之后会在用户的根目录生成一个 “.ssh”的文件夹进入“.ssh”会生成以下几个文件authorized_keys:存放远程免密登录的公钥,主要通过这个文件记录多台机器的公钥id_rsa : 生成的私钥文件id_rsa.pub &#xff1a; 生成的公钥文件…

Android studio aar包多层嵌套,Add library ‘Gradle: __local_aars__: 。。。unspecified@jar‘ to classpath

编译出错报的日志&#xff1a; FAILURE: Build failed with an exception. * What went wrong: Execution failed for task :toolbox:compileReleaseJavaWithJavac. > Compilation failed; see the compiler error output for details. 出错的代码提示&#xff1a; Add …

约瑟夫环数数

题目&#xff1a; 有n个孩子站成一圈&#xff0c;从第一个孩子开始顺时针方向报数&#xff0c;报到3的人出列&#xff0c;下一个人继续从1报数&#xff0c;直到最后剩下一个孩子为止。问剩下第几个孩子。 (另一种题型 -> 约瑟夫环 -> 递归算法 http://www.cnblogs.com/…

eclipse 中文_谁说API必须用英文?中文API的Java库可以有!

是不是看惯了文档里的英文接口(API)&#xff0c;也在 IDE 里看惯了自动补全里的英文接口&#xff1f;现今的绝大多数 API 的确是英文命名没错&#xff0c;但绝非不能或者不应该实现和发布中文 API 的库。这里用一个简单的汉字简繁转换库为例。在命名上&#xff0c;API 的方法名…

C# FTP 上传、下载、获取文件列表

public class FtpHelper{string ftpServerIP;string ftpRemotePath;string ftpUserID;string ftpPassword;string ftpURI;/// <summary>/// 连接FTP/// </summary>/// <param name"FtpServerIP">FTP连接地址</param>/// <param name"…

springboot接收json参数_Springboot + Vue + shiro 实现前后端分离、权限控制

小Hub领读&#xff1a;嘿嘿&#xff0c;之前我也发了一篇类似的项目&#xff0c;SpringBootVue的项目&#xff0c;还有视频讲解&#xff0c;如果这篇文章看完不懂&#xff0c;不妨去看看我的视频讲解哈&#xff0c;超级详细&#xff01;太赞了&#xff0c;SpringBootVue前后端分…

python带参数的装饰器_Python-----带参数的装饰器以及补充

带参数的装饰器 def wrapper_out(n): # def wrapper(f): # def inner(*args,**kwargs): # # if n qq: # # username input(请输入用户名&#xff1a;).strip() # # password input(请输入密码&#xff1a;).strip() # # with open(qq,encodingutf-8) as f1: # # for line in…

太极图python自定义函数绘制_[宜配屋]听图阁

效果如下所示&#xff1a;# -*- coding: utf-8 -*- import turtle # 绘制太极图函数 def draw_TJT(R): turtle.screensize(800, 600, "green") # 画布长、宽、背景色 长宽单位为像素 turtle.pensize(1) # 画笔宽度 turtle.pencolor(black) # 画笔颜色 turtle.speed(1…

商业

不出意外锤子要被收购&#xff0c;锤子给大家上了一课&#xff0c;商业首先要满足大众需求 然后在此基础上有更多的追求&#xff0c;品质、设计、优质的软件体验这些都没错&#xff0c;但更重 要的是商业逻辑。先能很好地满足当下的需求&#xff0c;指纹、4g这些明显优先级高的…