android - 如何从android中的手机内存中读取文件?

标签 android memory readfile

我已经使用 android 中的 FileOutputStreamHttpConnection 下载了一个文件,现在它被写入手机内存中的路径,就像我在 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();
}

最佳答案

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

Similar post here 

read file from phone memory

关于android - 如何从android中的手机内存中读取文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2200545/

相关文章:

java - 如何在更改全屏/底部导航时使用导航组件 navhostfragment

java - 如何在android中获取位置名称

c++ - 在 C++ 中寻找类似 getrusage() 的东西

python - 加载 pickle 的 python 对象使用大量内存

android - 在android中解析JSON对象

c# - Android 应用程序和 ASP.net api 之间的安全通信

c - 为什么我的程序只能在调试器中运行?

java - 如何访问Java数组中的第一列数据并分配给另一个变量?

Node.js Web 服务器 fs.createReadStream 与 fs.readFile?

c++ - 对 ReadFile 函数的异步调用返回 6 错误代码