幾周以來,我們一直在處理之前討論過的一個問題,但在我們遇到的特定情況下沒有找到有效的答案。因此,在重新閱讀幾十個線程並嘗試提供所有代碼人之後,請求您的幫助。 (順便說一句,忘記問三星:他們證明比有用的方式少。)三星文件io - 可以寫文件但不能回讀
OS:通過4.2.x版(API lvls 14 - 17)4.0.x的
設備:三星S3具有/存儲/ sdcard0(我們測試的所有事情。)
...而不是舊到/ mnt/SD卡或/存儲/ SD卡(注缺乏尾隨零)。
使用
Environment.getExternalStorageDirectory().getAbsolutePath();
// e.g .getPath() .getName() etc.
// or
Environment.getExternalStoragePublicDirectory() ...
任何味道或使用媒體URI通知文件位置的系統。 (令人震驚的是,這也失敗了。)
現在保存一個文件 - 你會看到它顯示在DDMS文件瀏覽器中。你可以通過亞行來驗證。但是,只要嘗試重新讀取同一個文件即可 - 使用與首先編寫的應用程序相同的應用程序! ENOENT - 文件未找到。硬編碼的路徑,追加'/ external_sd /'到上面的os調用給出的路徑(Sumsung說這是需要的 - 但它不會蹲下。)
將'sdcard0'更改爲'sdcard' ...也嘗試了這一切。小人物。
權限等當然都是正確的(因爲一個進程什麼時候可以寫一個文件但不能讀取它?)。
USB電纜連接與否,調試模式或沒有,「真正的應用程序」與開發者的應用程序(不受信任的應用程序) - 結果都是一樣的:ENOENT)
對此處進行的任何意見或建議?
(手裏拿着大錘,在一個新的SG3目不轉睛地盯着......而且,SAMSUNG,如果你正在讀這篇文章:「/storage/sdcard0/external_sd/myFileFoo.txt」不起作用。)
/**
*
* [Edit - added sample of failing code, as requested]
*/
public void testFile() {
ImageView image ;
String m_Path = "/SamsuxS3/" ; // more fun than a barrel of NULLs
String m_MyFile = "myFileFoo.jpg" ;
image = (ImageView) findViewById (R.id.imageView1) ;
//// Test 0:
m_Path = Environment.getExternalStorageDirectory().getAbsolutePath() ;
//// Test 1:
// String getPath = Environment.getExternalStorageDirectory().getPath(); // fails
// m_Path = getPath ;
//// Test 2:
// String getName = Environment.getExternalStorageDirectory().getName() ; //fails
// m_Path = getName ;
//// Test 3:
// String defPics = Environment.DIRECTORY_PICTURES; // fails
// m_Path = m_path + "/" + defPics + "/" ;
//// Test 4:
// m_Path = "/storage/sdcard0/" ; // fails
//// Test 5:
// m_Path = "/storage/sdcard0/external_sd/" // Samsung says so, but it fails too.
//// Test 6: now we're really hacking ...
// m_Path = "/storage/sdcard/" // Fails (although sdcard is mounted as sdcard0 - hmmm)
InputStream fIn = null;
File fileIn = new File(m_Path, m_MyFile);
try { //// This is only one way many attempts...
//// 1) just grab an image from a known resource,
//// 2) try to save it,
//// 3) then read it back into an ImageView.
//// External storage must be mounted or this fails.
InputStream is = getResources().openRawResource(R.drawable.somepicture) ; // works
OutputStream os = new FileOutputStream(fileIn); // OK
byte[] data = new byte[is.available()]; // OK
is.read(data); // OK
os.write(data); // OK - DDMS file explorer verfied
is.close();
os.close();
} catch (IOException e) {
Log.d("Error writing to " + m_Path, e.toString()); // never happened yet
}
//// now we step into the SamDung
////
InputStream fIn2 = null; //// Well, it's redundant but ...
File fileIn2 = new File(m_Path, m_MyFile);
try {
fIn2 = new FileInputStream (fileIn2) ;
//
// Here be the Dragons...
//
// Next line WORKS on every device EXCEPT a Samsung - blows up w/ ENOENT !
//
image.setImageBitmap (BitmapFactory.decodeStream(fIn2));
fIn2.close();
} catch (Exception IOError) {
Log.d("WTF? I'm not moving to Korea: ", IOError.toString()) ;
}
}
發表演示您的問題的示例應用程序。 – CommonsWare 2013-05-09 00:37:30
認爲這很清楚,但正如您所請求的......請參閱OP底部的編輯。感謝 -^h – 2013-05-09 01:56:45
這更多,如果我要掏出$ 400 +買設備,試圖重現你的問題,它會是很好**非常非常肯定**,我沒有浪費我的時間和錢。這就是爲什麼我要求 - 並繼續要求 - 爲**示例應用程序**(即完整的Android項目)。IOW,如果你希望別人能夠重現你的問題,試圖幫助你克服它,那麼你可以給我們的東西越清楚,就會越清楚。 – CommonsWare 2013-05-09 10:53:31