三星文件io - 可以写文件但不能读回来

时间:2013-05-09 00:36:09

标签: android file-io file-permissions

几个星期以来,我们一直在讨论之前已经讨论过的问题,但在我们遇到过的具体案例中没有得到合适的答案。因此,在重新阅读了几十个主题并尝试提供所有代码的人之后,请求我帮忙。 (顺便说一句,忘记询问三星:他们证明了这一点并不乐于帮助。)

操作系统:4.0.x到4.2.x(API lvls 14 - 17)

设备三星S3 有/ storage / sdcard0(我们测试的都是。)

...而不是旧的 / mnt / sdcard / storage / sdcard (注意缺少尾随零)。

使用任何风味的

Environment.getExternalStorageDirectory().getAbsolutePath(); 
// e.g .getPath() .getName() etc.

// or

Environment.getExternalStoragePublicDirectory() ... 

或使用媒体URI通知系统文件位置。 (令人震惊的是,这也失败了。)

现在保存文件 - 您将看到它显示在DDMS文件资源管理器中。您可以通过ADB将其拉出来进行验证。但只是尝试重读相同的文件 - 使用您首先编写的相同应用程序! ENOENT - 找不到档案。硬编码路径,追加' / external_sd /' os调用上面给出的路径(Sumsung说这是必要的 - 但它并没有蹲下。)

更改' sdcard0'到了'sdcard'正如一些人所建议的......也尝试过这一切。小人物。

权限等当然都是正确的(因为什么时候进程可以写一个文件而不能读它!?)。

是否连接了USB线,是否有调试模式,"真正的应用程序" vs开发者应用程序(不受信任的应用程序) - 结果完全相同: ENOENT

有关如何在此处进行的任何想法或建议?

(拿着大锤,专心地盯着新的SG3 ......而且,三星,如果你正在读这个:" /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()) ; 
        }  
  }      

1 个答案:

答案 0 :(得分:3)

Android 3.0的proper way to write a file on Android,您打算再次快速使用该文件,是:

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

表示名为FileOutputStream的{​​{1}}。

使用此更改,您的示例代码可与运行Android 4.1.2的Samsung Galaxy S3上的os配合使用。

相关问题