2013-05-09 37 views
0

幾周以來,我們一直在處理之前討論過的一個問題,但在我們遇到的特定情況下沒有找到有效的答案。因此,在重新閱讀幾十個線程並嘗試提供所有代碼人之後,請求您的幫助。 (順便說一句,忘記三星:他們證明比有用的方式少。)三星文件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()) ; 
     } 
    }  
+0

發表演示您的問題的示例應用程序。 – CommonsWare 2013-05-09 00:37:30

+0

認爲這很清楚,但正如您所請求的......請參閱OP底部的編輯。感謝 -^h – 2013-05-09 01:56:45

+0

這更多,如果我要掏出$ 400 +買設備,試圖重現你的問題,它會是很好**非常非常肯定**,我沒有浪費我的時間和錢。這就是爲什麼我要求 - 並繼續要求 - 爲**示例應用程序**(即完整的Android項目)。IOW,如果你希望別人能夠重現你的問題,試圖幫助你克服它,那麼你可以給我們的東西越清楚,就會越清楚。 – CommonsWare 2013-05-09 10:53:31

回答

3

proper way to write a file on Android爲Android 3.0以上版本,在那裏你打算很快再次使用的文件,方法是:

os.flush(); 
    os.getFD().sync(); 
    os.close(); 

FileOutputStream命名os

使用此更改,您的示例代碼適用於運行Android 4.1.2的Samsung Galaxy S3上的Environment.getExternalStorageDirectory()

+0

感謝您的迴應,但快速讀取文件並不是問題......它使用「已知」路徑和文件名將它重新讀回_at all_。三星公司已經錯誤地宣佈,我們必須在返回的路徑中附加「external_sd /」 - 無論該路徑是通過String〜.getAbosultePath()還是File〜.getExternalStorageDirectory();即使他們自己的示例代碼在4.1.1上也失敗......還沒有嘗試過4.2.2。 – 2013-05-18 03:58:41

+0

@HowardPautz:無論如何請嘗試配方。我能夠重現你的問題,並且這個配方修復了它。 – CommonsWare 2013-05-18 06:19:47

+0

謝謝,正如我在OP中所說的,我嘗試了很多東西,包括與您的食譜相同的東西。出於您對我的努力的考慮,我非常感謝,當然,我之前在回覆之前嘗試了您的食譜(「再次」)。它仍然不起作用。 ......三星想要淘汰的強風 - 谷歌谷歌,傾銷Android以支持Tizen ......我們的痛苦纔剛剛開始......如果您不相信這些謠言,請嘗試使用他們的「webkit」。 – 2013-05-20 22:33:50