X-Git-Url: http://deadsoftware.ru/gitweb?p=d2df-sdl.git;a=blobdiff_plain;f=android%2Fsrc%2Forg%2Fd2df%2Fapp%2FCopyAssets.java;h=f5f6c67e81a13c33decd8bf0e253fe687272f68a;hp=445f30a80644fc052b45715e668963a2d63ffebd;hb=2f2f9c997a76f5394c4deb2fa7d54b99a6186c87;hpb=c97b4f7894fd3d797ecea3de574b58499c43aa85 diff --git a/android/src/org/d2df/app/CopyAssets.java b/android/src/org/d2df/app/CopyAssets.java index 445f30a..f5f6c67 100644 --- a/android/src/org/d2df/app/CopyAssets.java +++ b/android/src/org/d2df/app/CopyAssets.java @@ -2,6 +2,7 @@ package org.d2df.app; import android.content.Context; import android.content.res.AssetManager; +import android.os.Environment; import java.io.File; import java.io.FileOutputStream; @@ -13,59 +14,81 @@ import android.util.Log; public class CopyAssets { - public static void copyAssets(Context context, String prefix) { - AssetManager assetManager = context.getAssets(); - String[] files = null; - try { - files = assetManager.list(prefix); - } catch (IOException e) { - Log.e("tag", "Failed to get asset file list.", e); - } - if (files != null) for (String filename : files) { - InputStream in = null; - OutputStream out = null; - try { - File f = new File(context.getExternalFilesDir(null), prefix); - if (!f.exists()) { - f.mkdirs(); - } - File outFile = new File(context.getExternalFilesDir(null), prefix + "/" + filename); - if (!outFile.exists()) { - in = assetManager.open(prefix + "/" + filename); - out = new FileOutputStream(outFile); - copyFile(in, out); - } - } catch(IOException e) { - Log.e("tag", "Failed to copy asset file: " + filename, e); - } - finally { - if (in != null) { - try { - in.close(); - in = null; - } catch (IOException e) { - - } - } - if (out != null) { - try { - out.flush(); - out.close(); - out = null; - } catch (IOException e) { - - } - } - } - } - } - - public static void copyFile(InputStream in, OutputStream out) throws IOException { - byte[] buffer = new byte[1024]; - int read; - while((read = in.read(buffer)) != -1){ - out.write(buffer, 0, read); - } - } - + private static boolean ExtStorageMounted() { + return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); + } + + private static boolean ExtStorageReadonly() { + return Environment.MEDIA_MOUNTED_READ_ONLY.equals(Environment.getExternalStorageState()); + } + + private static void CopyFile(InputStream in, OutputStream out) + throws IOException + { + byte[] buffer = new byte[1024]; + int read; + while ((read = in.read(buffer)) != -1) { + out.write(buffer, 0, read); + } + } + + public static void copyAssets(Context context, String prefix) { + AssetManager assetManager = context.getAssets(); + String[] files = null; + try { + files = assetManager.list(prefix); + } catch (IOException e) { + Log.e("tag", "Failed to get asset file list.", e); + } + if (files != null) { + for (String filename : files) { + InputStream in = null; + OutputStream out = null; + try { + if (ExtStorageMounted() && !ExtStorageReadonly()) { + /* Get External Storage Path */ + File f = new File(context.getExternalFilesDir(null).getAbsolutePath(), prefix); + if (!f.exists()) { + f.mkdirs(); + } + File outFile = new File(context.getExternalFilesDir(null).getAbsolutePath(), prefix + "/" + filename); + if (!outFile.exists()) { + in = assetManager.open(prefix + "/" + filename); + out = new FileOutputStream(outFile); + CopyFile(in, out); + } + } else { + /* Get Internal Storage Path */ + File f = new File(context.getFilesDir().getAbsolutePath(), prefix); + if (!f.exists()) { + f.mkdirs(); + } + File outFile = new File(context.getFilesDir().getAbsolutePath(), prefix + "/" + filename); + if (!outFile.exists()) { + in = assetManager.open(prefix + "/" + filename); + out = new FileOutputStream(outFile); + CopyFile(in, out); + } + } + } catch(IOException e) { + Log.e("tag", "Failed to copy asset file: " + filename, e); + } finally { + if (in != null) { + try { + in.close(); + in = null; + } catch (IOException e) {} + } + if (out != null) { + try { + out.flush(); + out.close(); + out = null; + } catch (IOException e) {} + } + } + } + } + } + }