相机拍照时会根据选择的保存路径建个DCIM/camera的目录来存放照片,但是当选择已经被写满的外置sd卡时目录就无法创建了,即new StatFs(SDCard.instance().getDirectory())的参数无效,所以才会报出如标题的bug.
private static long getSDCardAvailableSpace() { if (SDCard.instance().isWriteable()) { File dir = new File(SDCard.instance().getDirectory()); dir.mkdirs(); try { StatFs stat = new StatFs(SDCard.instance().getDirectory()); long ret = stat.getAvailableBlocks() * (long) stat.getBlockSize(); Log.d(TAG, "ret---"+ret); return ret; } catch (Exception e) { Log.d(TAG,"not get ret exception---"+e.toString()); } return UNKNOWN_SIZE; } return UNKNOWN_SIZE; }
代码变成如下即可:
private static long getSDCardAvailableSpace() { if (SDCard.instance().isWriteable()) { File dir = new File(SDCard.instance().getDirectory()); dir.mkdirs(); if (dir.exists()) { try { StatFs stat = new StatFs(SDCard.instance().getDirectory()); long ret = stat.getAvailableBlocks() * (long) stat.getBlockSize(); return ret; } catch (Exception e) { Log.d(TAG,"not get ret exception---"+e.toString()); } return UNKNOWN_SIZE; }else { return LOW_STORAGE_THRESHOLD_BYTES; } } return UNKNOWN_SIZE; }