如何从android内部存储中获取文件的文件路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20280250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get file path of file from internal storage in android
提问by Aarushi
In our application we are storing pdf files into internal storage.Now i want to get its filepath and need to store into DB. Please tell me how to get its file path. below code:
在我们的应用程序中,我们将 pdf 文件存储到内部存储中。现在我想获取其文件路径并需要存储到 DB 中。请告诉我如何获取其文件路径。下面的代码:
public void storeIntoInternalMem(byte[] databytes, String name) {
try
{
FileOutputStream fileOuputStream = openFileOutput(name, MODE_PRIVATE);
fileOuputStream.write(databytes);
fileOuputStream.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
回答by d3m0li5h3r
you can use Context.getFilesDir()
method to get the path to the internal storage
您可以使用Context.getFilesDir()
方法来获取内部存储的路径
Edit
编辑
when using this line FileOutputStream fileOuputStream = openFileOutput(name, MODE_PRIVATE);
the internal storage path is obtained by the default implementation of the openFileOutput()
method. A new file is created at that location with the specified name. Now when you want to get the path of the same file, you can use the getFilesDir()
to get the absolute path to the directory where the file was created. You can do something like this :-
使用此行时FileOutputStream fileOuputStream = openFileOutput(name, MODE_PRIVATE);
,内部存储路径是通过该openFileOutput()
方法的默认实现获得的。在该位置创建一个具有指定名称的新文件。现在,当您想要获取同一文件的路径时,可以使用getFilesDir()
来获取创建文件的目录的绝对路径。你可以做这样的事情:-
File file = new File(getFilesDir() + "/" + name);
File file = new File(getFilesDir() + "/" + name);
回答by VikingGlen
String dir = getFilesDir().getAbsolutePath();
String dir = getFilesDir().getAbsolutePath();
回答by Droid Teahouse
getFileStreamPath(String name) of Context.
Returns the absolute path on the filesystem where a file created with
返回文件系统上创建文件的绝对路径
openFileOutput(String, int)
is stored.
被储存了。