Solution 1 :
Documentation for getExternalCacheDirs()
clearly states that it returns an array of File
containing:
File[] – the absolute paths to application-specific directories. Some
individual paths may be null if that shared storage is not currently
available. The first path returned is the same as
getExternalCacheDir().
So you need to check each entry in the returned array for null
. In your case, you are getting a null
entry in the array which causes this exception.
Problem :
I have a problem with memory access. The strangest thing is that this error is not even find in the internet. This bug happened on device with android 11.
The method with which the problem is related:
void initSaveDataFolder(){
if(SecurityPreferences.getAutoChosseFolder())
for (File file : App.get().getExternalCacheDirs()) {
try {
if (Environment.isExternalStorageRemovable(file)) {
//System.out.println("getExternalCacheDirs is "+new Gson().toJson(file));
StatFs stat = new StatFs(file.getAbsolutePath());
long bytesAvailableInSdCard = (long)stat.getBlockSize() *(long)stat.getAvailableBlocks();
StatFs stat2 = new StatFs( App.get().getExternalCacheDir().getAbsolutePath());
long bytesAvailableInInternalMemory = (long)stat2.getBlockSize() *(long)stat2.getAvailableBlocks();
if(bytesAvailableInSdCard>bytesAvailableInInternalMemory) SecurityPreferences.setIsSaveDataOnUsb(true);
else SecurityPreferences.setIsSaveDataOnUsb(false);
System.out.println("memoryes is "+ bytesAvailableInSdCard+" "+bytesAvailableInInternalMemory);
SecurityPreferences.saveAutoChosseFolder(true);
break;
}
}catch (NullPointerException e){
SecurityPreferences.setIsSaveDataOnUsb(false); break;
}
}
}
Permissions related to storage access:
<uses-permission
android_name="android.permission.WRITE_EXTERNAL_STORAGE"
tools_ignore="ScopedStorage" />
<uses-permission android_name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android_name="android.permission.READ_INTERNAL_STORAGE" />
And Error stack trace:
Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.estvisx.ma/com.estvisx.ma.ui.splash.SplashActivity}: java.lang.IllegalArgumentException: Failed to find storage device at null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3431)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3595)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7664)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by java.lang.IllegalArgumentException: Failed to find storage device at null
at android.os.Environment.isExternalStorageRemovable(Environment.java:1202)
at com.estvisx.ma.ui.splash.SplashActivity.initSaveDataFolder(SplashActivity.java:297)
at com.estvisx.ma.ui.splash.SplashActivity.onCreate(SplashActivity.java:83)
at android.app.Activity.performCreate(Activity.java:7994)
at android.app.Activity.performCreate(Activity.java:7978)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1534)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3404)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3595)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7664)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
I tried to search about this problem whole day, but i didnt find anything. I just have no idea.
Comments
Comment posted by blackapps
have a problem with memory access.