NPE on Environment.getExternalStorageState()?

馋奶兔 提交于 2019-12-04 22:39:40

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.

Duanxing

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.

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