NPE on Environment.getExternalStorageState()?

丶灬走出姿态 提交于 2019-12-06 16:11:06

问题


Cannot find the reason why I am getting "Null pointer exception" . I could give an explicit check to see if "getStorageState is null" but that doesnt explain why I am getting this error. The error is :

java.lang.NullPointerException
at android.os.Environment.getStorageState(Environment.java:719)
at android.os.Environment.getExternalStorageState(Environment.java:694)
at com.ciqual.android.insight.sessionService.RemoveFiles(SessionService.java:664)
com.vyshas.android.sessionService.onEndSession(SessionService.java:460)

at : (the line that error points to is this :)(seen in jellybean 4.3 and kitkat)

if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            //
        }

回答1:


Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState) wouldnt cause a NPE while Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) does cause NPE and crashes the application.

I still dont know why getExternalStorageState is null eventhough I have permissions set but the solutions atleast wouldn't crash the application.




回答2:


I have similar repot from Android 4.3 .

Source code is:

    /**
     * Gets the current state of the primary "external" storage device.
     *
     * @see #getExternalStorageDirectory()
     */
    public static String getExternalStorageState() {
        try {
            IMountService mountService = IMountService.Stub.asInterface(ServiceManager
                    .getService("mount"));
            final StorageVolume primary = getPrimaryVolume();
            return mountService.getVolumeState(primary.getPath());
        } catch (RemoteException rex) {
            Log.w(TAG, "Failed to read external storage state; assuming REMOVED: " + rex);
            return Environment.MEDIA_REMOVED;
        }
    }

I have nothing to do but try catch it.



来源:https://stackoverflow.com/questions/23115385/npe-on-environment-getexternalstoragestate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!