简体   繁体   English

FileNotFoundException仅在某些设备上

[英]FileNotFoundException on some devices only

I am trying to create file on the external directory like this 我正在尝试像这样在外部目录上创建文件

File sd = Environment.getExternalStorageDirectory();
if (sd == null) {
    Toast.makeText(mContext,
            "External Storage does not exist..No Export",
            Toast.LENGTH_LONG).show();
    return false;
}

File dataDir = new File(sd.getAbsolutePath()+ "/"+ DBHandler.folderName);
if(!dataDir.exists()){
    dataDir.mkdir();
}

File wbfile = new File(dataDir.getAbsolutePath()+ "/"+filename+".xls");

but I get the following on few devices 但是我在一些设备上得到以下信息

java.io.FileNotFoundException: /storage/emulated/0/AplicationXX Data/report.xls: open failed: ENOENT (No such file or directory)
    at libcore.io.IoBridge.open(IoBridge.java:409)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
    FileOutputStream out  = new FileOutputStream(wbFile);

Any idea what could be the reason? 知道是什么原因吗?

You have to check your device storage(internal or external) as below 您必须按以下方式检查设备存储空间(内部或外部)

boolean mExternalStorageAvailable ;
boolean mExternalStorageWriteable ;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
    mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
    mExternalStorageAvailable = true;
    mExternalStorageWriteable = false;
} else {
    mExternalStorageAvailable = mExternalStorageWriteable = false;
    //internalstorage stuff
}

and add permission in android manifest file, 并在android清单文件中添加权限,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM