无法创建新目录并将资源添加到Android目录中

时间:2014-03-27 03:18:31

标签: android directory storage assets mkdir

好的,所以在我的应用程序中,我将图像图标添加到用户SD卡,以便他们可以更改他们想要的任何一个。我试图将一个文件夹添加到他们的外部存储器,我的资产文件夹中的图像将复制到该文件夹​​中。无论我尝试什么,没有任何副本或图像直接复制到SD卡而不创建文件夹。

继承人我得到了什么:

//使用的缓冲区大小。     private final static int BUFFER_SIZE = 8192;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try {
        AssetManager assetFiles = getAssets();

        // MyHtmlFiles is the name of folder from inside our assets folder
        String[] files = assetFiles.list("Depth Slick Icons");

        // Initialize streams
        InputStream in = null;
        OutputStream out = null;

        for (int i = 0; i < files.length; i++) {

            if (files[i].toString().equalsIgnoreCase("Depth Slick Icons")
                    || files[i].toString().equalsIgnoreCase("js")) {

                /*
                 * @Do nothing. images and js are folders but they will be
                 * interpreted as files.
                 * 
                 * @This is to prevent the app from throwing file not found
                 * exception.
                 */

            } else {

                /*
                 * @Folder name is also case sensitive
                 * 
                 * @MyHtmlFiles is the folder from our assets
                 */
                in = assetFiles.open("Depth Slick Icons/" + files[i]);

                /*
                 * Currently we will copy the files to the root directory
                 * but you should create specific directory for your app
                 */
                out = new FileOutputStream(
                        Environment.getExternalStorageDirectory() + "Depth Icons/" + files[i]);
                copyAssetFiles(in, out);

            }
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

private static void copyAssetFiles(InputStream in, OutputStream out) {
    try {

        byte[] buffer = new byte[BUFFER_SIZE];
        int read;

        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }

        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;

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

}

和是权限在清单中设置。

&#34;深度光滑图标&#34;是资产中的文件夹和&#34;深度图标&#34;是我试图创建放置图像的文件夹。

1 个答案:

答案 0 :(得分:0)

您需要先使用mkdir()函数在Depth Slick Icons内创建目录。

http://developer.android.com/reference/java/io/File.html#mkdir%28%29