copy Assets To Internal Storage - Android App

Android examples for App:Assets

Description

copy Assets To Internal Storage

Demo Code

/*//w w  w  . ja  v  a 2 s. co m
 * Copyright 2013 The Kava Project Developers. See the COPYRIGHT file at the top-level directory of this distribution
 * and at http://kavaproject.org/COPYRIGHT.
 *
 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the
 * MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option. This file may not be copied,
 * modified, or distributed except according to those terms.
 */
//package com.java2s;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.res.AssetManager;

public class Main {
  public static boolean copyAssetsToInternalStorage(File assetFile,
      File storageFile, AssetManager assetManager) {
    try {
      String[] assets = assetManager.list(assetFile.getPath());
      if (assets.length == 0) {
        return copyFile(assetFile, storageFile, assetManager);
      } else {
        storageFile.mkdir();
        for (String file : assets) {
          copyAssetsToInternalStorage(new File(assetFile, file), new File(
              storageFile, file), assetManager);
        }
      }
      return true;
    } catch (IOException ex) {
      return false;
    }
  }

  private static boolean copyFile(File assetFile, File storageFile,
      AssetManager assetManager) {
    try {
      InputStream in = assetManager.open(assetFile.getPath());
      FileOutputStream out = new FileOutputStream(storageFile);
      byte[] buffer = new byte[1024];
      int read;
      while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
      }
      in.close();
      out.close();
      return true;
    } catch (FileNotFoundException e) {
      return false;
    } catch (IOException e) {
      return false;
    }
  }
}

Related Tutorials