如何在Android中从手机内存中读取文件?
我已经使用 android 中的 FileOutputStream
从 HttpConnection
下载了一个文件,现在它被写入手机内部存储器的路径中,就像我在文件资源管理器
中找到的那样>
/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();
}
I have downloaded a file from HttpConnection
using the FileOutputStream
in android and now its being written in phone's internal memory on path as i found it in File Explorer
/data/data/com.example.packagename/files/123.ics
Now, I want to open & read the file content from phone's internal memory to UI. I tried to do it by using the FileInputStream
, I have given just filename with extension to open it but I am not sure how to mention the file path for file in internal memory,as it forces the application to close.
Any suggestions?
This is what I am doing:
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从手机内存读取文件
read file from phone memory
如果文件位于您所说的位置,并且您的应用程序是
com.example.packagename
,则调用openFileInput("123.ics");
将返回一个 < code>FileInputStream 在有问题的文件上。或者,调用
getFilesDir()
获取指向/data/data/com.example.packagename/files
的File
对象,并从那里。If the file is where you say it is, and your application is
com.example.packagename
, then callingopenFileInput("123.ics");
will return you aFileInputStream
on the file in question.Or, call
getFilesDir()
to get aFile
object pointing to/data/data/com.example.packagename/files
, and work from there.我正在使用此代码打开内部存储中的文件。我想我可以帮忙。
I am using this code to open file in internal storage. i think i could help.