3

我已经从HttpConnection使用FileOutputStreamin android下载了一个文件,现在它被写入手机的内存路径中,因为我在File Explorer

/data/data/com.example.packagename/files/123.ics

现在,我想打开并从手机内存读取文件内容到 UI。我试图通过使用来做到这一点FileInputStream,我只给出了带有扩展名的文件名来打开它,但我不确定如何提及内部存储器中文件的文件路径,因为它会强制应用程序关闭。

有什么建议么?


这就是我正在做的事情:

try
{               
  FileInputStream fileIn;       
  fileIn = openFileInput("123.ics");
  InputStream in = null;
  EditText Userid = (EditText) findViewById(R.id.user_id);
  byte[] buffer = new byte[1024];
  int len = 0;
  while ( (len = in.read(buffer)) > 0 )         
  {     
     Userid.setText(fileIn.read(buffer, 0, len));
  }                    
  fileIn.close();                       
} catch (FileNotFoundException e)
{
   e.printStackTrace();
}
catch (IOException e)
{   
    e.printStackTrace();
}
4

3 回答 3

6
String filePath = context.getFilesDir().getAbsolutePath();//returns current directory.
File file = new File(filePath, fileName);

Similar post here 

从手机内存中读取文件

于 2011-06-02T16:51:59.463 回答
4

如果文件在您所说的位置,并且您的应用程序是com.example.packagename,那么调用openFileInput("123.ics");将返回FileInputStream有关文件的 a 。

或者,调用getFilesDir()以获取File指向 的对象/data/data/com.example.packagename/files,然后从那里开始工作。

于 2010-02-04T14:57:58.423 回答
-2

我正在使用此代码在内部存储中打开文件。我想我可以帮忙。

File str = new File("/data/data/com.xlabz.FlagTest/files/","hello_file.xml");
于 2011-02-03T12:41:31.347 回答