2011-11-07 118 views
1

我有一個路由設備,當我做到這一點無法讀取內部存儲的文件(文件不存在問題)

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"); 
} 
+0

ü沒有添加以下權限到您的應用程序: <使用許可權的android:NAME = 「android.permission.BLUETOOTH」/> <使用許可權的android:名稱= 「android.permission。BLUETOOTH_ADMIN「/> –

+0

我不試圖訪問藍牙,我只是想從內存中讀取文件 – Lukap

回答

2

如果該文件是在SD卡,嘗試:

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

也嘗試添加:

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

外部<application></application>在您的清單文件中。

編輯
如果該文件是在內部存儲器:您的應用程序可以從內部存儲器中一個特殊的文件夾只讀。該文件夾的路徑返回爲: getFilesDir()。getAbsolutePath()

因此,把文件放在那裏,並用openFileInput()讀取它。

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

+0

對不起,不工作:(有沒有人從任何/ data/misc/files中讀取內容? – Lukap

+0

手機內存或sdcard根目錄上的'/ data /'文件夾?如果它位於手機內存的根目錄你的應用程序無法訪問它,把它放在SD卡上 – Caner

+0

它在手機上的內部存儲是問題,是否有可能讀取這個文件,如果沒有的話,是否有任何解決方法? – Lukap

0

從文檔爲File ...

公共文件(字符串dirPath,字符串名稱)

的構造方法使用指定的目錄路徑和文件的新文件名稱,在兩者之間放置一個路徑分隔符。

在你的代碼使用...

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

...並因爲你dirPath用隔離"/data/misc/bluetooth/"結束它會導致兩個分隔符。換句話說,有效的路徑將是...

/data/misc/bluetooth//dynamic_auto_pairing.conf

注意//後「bluetooth`

+0

請參閱我的編輯 – Lukap

+0

你甚至設法讀取放在/ data/misc /文件夾中的內容表單文件嗎? – Lukap