簡體   English   中英

文件目錄不存在context.getFilesDir()

[英]File directory doesn't exist with context.getFilesDir()

作為I / O的新手,我已經閱讀並嘗試使用thisthis中的示例。 但是,我只是無法在內部存儲中創建一個簡單的目錄。 一定是某個地方的錯字。

對於我的應用程序,我已經使用以下file_paths.xml定義了FileProvider,用於存儲應用程序內部文件。

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <files-path name="ma_pics" path="pictures/" />
</paths>

下面一個簡單的方法應該提供一個位於“圖片”目錄中的文件,但是我的問題是,當我嘗試創建目錄時,該目錄根本不存在。 我究竟做錯了什么?

@Nonnull
public final File providePictureFile(@Nonnull String prefix, @Nonnull String suffix)
            throws IOException {
        File picturesDir = new File(mContext.getFilesDir(), "pictures");
        if(!picturesDir.exists()){
            // code forks here, dir doesn't exist, throws the following exception
            throw new IOException("Pictures dir doesn't exist");
        }
        return new File(picturesDir, prefix + suffix);
    }

當我嘗試創建目錄時

您沒有創建目錄的任何代碼。 您正在創建一個File對象,該對象不會在文件系統上自動創建內容。 picturesDir上調用mkdirs()以創建該目錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM