gpt4 book ai didi

java - android.system.ErrnoException 错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:51:19 32 4
gpt4 key购买 nike

我正在尝试使用以下代码获取内部存储详细信息(用作内部存储器的存储)。

public void getinternalstorage() {
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
double bytesavalible = (double) stat.getBlockSizeLong() * (double) stat.getAvailableBlocksLong();
final double gb = bytesavalible / (1024 * 1024 * 1024);
double total = (double) stat.getBlockSizeLong() * stat.getBlockCountLong();
final double totalgb = total / (1024 * 1024 * 1024);
final double per = (100 - (gb / totalgb) * 100);
update_inte((int) (per * 1), round(totalgb, 2), round((gb), 2));
TextView text2written = (TextView) findViewById(R.id.inter);
text2written.setText(String.valueOf(round((totalgb - gb), 2)) + " GB " + "/ " + String.valueOf(round(totalgb, 2)) + " GB");
}

我也获得了所需的权限。

适用于 Android 5.0 及以下版本-

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

对于 android 6.0 及以上版本,我请求运行时权限。

但是我收到了来自大约 10 个用户的 android.system.ErrnoException 错误。完整的堆栈跟踪

Fatal Exception: java.lang.RuntimeException: Unable to resume activity {com.package.app.Main_Screen}: java.lang.IllegalArgumentException: Invalid path: /sdcard
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3121)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3152)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by java.lang.IllegalArgumentException: Invalid path: /sdcard
at android.os.StatFs.doStat(StatFs.java:46)
at android.os.StatFs.(StatFs.java)
at com.package.app.Main_Screen.getinternalstorage(Main_Screen.java:565)
at com.package.app.Main_Screen.on_resume(Main_Screen.java:785)
at com.package.app.Main_Screen.checkPermission(Main_Screen.java:1070)
at com.package.app.Main_Screen.onResume(Main_Screen.java:779)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1281)
at android.app.Activity.performResume(Activity.java:6320)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3110)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3152)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by android.system.ErrnoException: statvfs failed: ENOENT (No such file or directory)
at libcore.io.Posix.statvfs(Posix.java)
at libcore.io.BlockGuardOs.statvfs(BlockGuardOs.java:298)
at android.system.Os.statvfs(Os.java:500)
at android.os.StatFs.doStat(StatFs.java:44)
at android.os.StatFs.(StatFs.java)
at com.package.app.Main_Screen.getinternalstorage(Main_Screen.java:565)
at com.package.app.Main_Screen.on_resume(Main_Screen.java:785)
at com.package.app.Main_Screen.checkPermission(Main_Screen.java:1070)
at com.package.app.Main_Screen.onResume(Main_Screen.java:779)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1281)
at android.app.Activity.performResume(Activity.java:6320)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3110)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3152)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

我从 5.0 到 7.0 Android 版本收到此错误

最佳答案

I) 使用

从外部存储目录获取路径
Environment.getExternalStorageDirectory().getPath()

你会得到这样的东西:

 /storage/emulated/0
/mnt/sdcard
/mnt/sdcard-ext

但错误指出你只是gettin /sdcard:

Caused by java.lang.IllegalArgumentException: Invalid path: /sdcard at android.os.StatFs.doStat(StatFs.java:46) at android.os.StatFs.(StatFs.java)

如果我们检查引发此错误的 SDK 方法:

private static StructStatVfs doStat(String path) {
try {
return Os.statvfs(path);
} catch (ErrnoException e) {
throw new IllegalArgumentException("Invalid path: " + path, e);
}
}

函数statvfs()返回有关已安装的信息 文件系统。 path 是挂载的任何文件的路径名 文件系统。

因此,为避免此问题,您必须确保已安装 SD 卡!在这种情况下,很难向所有生产用户确保这一点,该选项将是一个验证。

顺便说一句,我推荐使用 getAbsolutePath()

Environment.getExternalStorageDirectory().getAbsolutePath();

代替

Environment.getExternalStorageDirectory().getPath();

要获取绝对路径,如果它是相对路径,则根据当前目录解析后的路径,从而产生“完全限定路径”。


II) 显示的其他错误信息是:

Caused by android.system.ErrnoException: statvfs failed: ENOENT (No such file or directory) at libcore.io.Posix.statvfs(Posix.java)

请记住,您的应用程序无法读取内部存储,请记住,您必须在设备 6.0+ 中手动要求WRITE_EXTERNAL_STORAGE(固有READ_EXTERNAL_STORAGE)权限

这可能是因为:

ENOENT (No such file or directory)

因为用户必须接受权限。

关于java - android.system.ErrnoException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47238691/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com