无法从内部存储读取文件(文件不存在问题)



我有一个路由设备,当我这样做时

adb shell  cat /data/misc/bluetooth/dynamic_auto_pairing.conf

打印该文件的内容。

但是在我的代码中,当我写这样的东西时,它说文件不存在。从控制台上我可以看到我知道它在那里,但从代码中我无法读取它。我的问题是问题出在哪里,是我漏掉了一些许可还是问题出在哪里?谁能给我提供一些代码从这个文件中读取内容。

感谢
File pa = new File("/data/misc/bluetooth/","dynamic_auto_pairing.conf");
//this doesn't works also
//File pa = new File("/data/misc/bluetooth","dynamic_auto_pairing.conf");
//File pa = new File("/data/misc/bluetooth/dynamic_auto_pairing.conf");
if(pa.exists()){
    Log.v("tag", "does exists");
}else{
    Log.v("tag", "does NOT exist");
}

如果文件在sd卡上,尝试:

File pa = new File(Environment.getExternalStorageDirectory() + "/data/misc/bluetooth/dynamic_auto_pairing.conf");

也可以尝试添加:

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

在你的manifest文件中<application></application>之外

编辑
如果文件在内存中:你的应用程序只能从内存中的一个特殊文件夹中读取。返回该文件夹的路径:.getAbsolutePath getFilesDir () ()

所以把文件放在那里,用openFileInput()读取。

更多信息:http://developer.android.com/guide/topics/data/data-storage.html filesInternal

From docs for File

public File (String dirPath, String name)

使用指定的目录路径和文件名构造一个新文件,在两者之间放置一个路径分隔符。

在你的代码中你使用了…

File pa = new File("/data/misc/bluetooth/","dynamic_auto_pairing.conf");

…并且因为您的dirPath以分隔符"/data/misc/bluetooth/"结束,它将导致两个分隔符。换句话说,有效路径将是…

/数据/misc/蓝牙//dynamic_auto_pairing.conf

注意" bluetooth "后面的//

如果你使用android 6.0或更高版本。您必须在代码中请求权限。

最新更新