如何在安卓系统中获取内部存储中文件的路径

21

我们的应用程序将pdf文件存储到内部存储中。现在我想获取其文件路径并需要将其存储到数据库中。请告诉我如何获取其文件路径。

以下是代码:

public void storeIntoInternalMem(byte[] databytes, String name) {
        try
        {
            FileOutputStream fileOuputStream = openFileOutput(name, MODE_PRIVATE);
            fileOuputStream.write(databytes);
            fileOuputStream.close();

        } catch (IOException e)
        {
            e.printStackTrace();
        }

    }

请参考以下链接:https://dev59.com/YmMm5IYBdhLWcg3whfWe - Milan Delvadia
4个回答

32

您可以使用Context.getFilesDir()方法获取内部存储路径。

编辑

当使用此行FileOutputStream fileOuputStream = openFileOutput(name, MODE_PRIVATE);时,内部存储路径是通过openFileOutput()方法的默认实现获取的。在该位置创建具有指定名称的新文件。现在,当您想要获取相同文件的路径时,可以使用getFilesDir()来获取创建文件的目录的绝对路径。您可以像这样执行操作:

File file = new File(getFilesDir() + "/" + name);


请简要回答一下这个问题好吗? - Aarushi
可以在我的手机上找到它。 - Mansuu....

12

String dir = getFilesDir().getAbsolutePath();


0
 getFileStreamPath(String name) of Context.

返回文件系统上创建的文件的绝对路径

openFileOutput(String, int) 

被存储。


0

关于 Kotlin:

"$filesDir/image.png"


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接