Example usage for android.content Context getAssets

List of usage examples for android.content Context getAssets

Introduction

In this page you can find the example usage for android.content Context getAssets.

Prototype

public abstract AssetManager getAssets();

Source Link

Document

Returns an AssetManager instance for the application's package.

Usage

From source file:org.kei.android.phone.netcap.OutputFragment.java

private static void copyFile(final String assetPath, final String localPath, final Context context) {
    try {//from   w  w w .jav a 2s .  c  o m
        final InputStream in = context.getAssets().open(assetPath);
        File f = new File(localPath);
        if (f.exists())
            f.delete();
        final FileOutputStream out = new FileOutputStream(f);
        int read;
        final byte[] buffer = new byte[4096];
        while ((read = in.read(buffer)) > 0) {
            out.write(buffer, 0, read);
        }
        out.close();
        in.close();

    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.adrguides.utils.HTTPUtils.java

public static JSONObject execGETMock(Context context, String address) throws IOException {

    Log.d("com.adrguides.utils.HTTPUtils", "loading");

    BufferedReader reader = new BufferedReader(
            new InputStreamReader(context.getAssets().open("mockguide.json"), "UTF-8"));

    StringBuffer jsontext = new StringBuffer();
    String line;//from   ww w. j  a  v  a 2s  .c  o m
    while ((line = reader.readLine()) != null) {
        jsontext.append(line).append('\n');
    }

    reader.close();

    try {
        Log.i("com.adrguides.utils.HTTPUtils", "result -> " + jsontext.toString());
        return new JSONObject(jsontext.toString());
    } catch (JSONException ex) {
        throw new IOException(MessageFormat.format("Parse exception: {0}.", ex.getMessage()));
    }
}

From source file:com.bourke.kitchentimer.utils.Utils.java

public static CharSequence readTextFile(Context context, String fileName) {
    BufferedReader in = null;//from  w  w  w . j  a  v a 2s  .com
    try {
        in = new BufferedReader(new InputStreamReader(context.getAssets().open(fileName)));
        String line;
        StringBuilder buffer = new StringBuilder();
        while ((line = in.readLine()) != null)
            buffer.append(line).append('\n');
        return buffer;
    } catch (IOException e) {
        Log.e("readTextFile", "Error readind file " + fileName, e);
        return "";
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                // Ignore
            }
        }
    }
}

From source file:com.futureplatforms.kirin.internal.attic.IOUtils.java

public static String loadTextAsset(Context activity, String filename) throws IOException {
    BufferedReader in = null;//from   w ww .j a v a 2  s.  c  o m
    try {

        in = new BufferedReader(new InputStreamReader(activity.getAssets().open(filename)));
        String line;
        StringBuilder buffer = new StringBuilder();
        while ((line = in.readLine()) != null) {
            buffer.append(line).append('\n');
        }
        return buffer.toString();
    } finally {
        close(in);
    }
}

From source file:com.swisscom.safeconnect.backend.PlumberTask.java

public static HttpClient getNewHttpClient(Context context, HttpParams params) {
    InputStream is = null;/*  www .  j a v a 2  s  .  c  o m*/
    try {
        synchronized (mKeystoreLock) {
            if (keyStore == null) {
                is = context.getAssets().open("swisscom.bks");
                keyStore = KeyStore.getInstance("BKS");
                keyStore.load(is, "sw1ssc0m".toCharArray());
            }

            if (sslSocketFactory == null) {
                sslSocketFactory = new SwisscomSslSocketFactory(keyStore);
                sslSocketFactory.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
            }

            if (schemeRegistry == null) {
                schemeRegistry = new SchemeRegistry();
                schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
                schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
            }

            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
            HttpProtocolParams.setContentCharset(params, "UTF-8");
        }

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, schemeRegistry);

        return new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        if (BuildConfig.DEBUG)
            Log.e(Config.TAG, "error", e);
        return new DefaultHttpClient();
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                if (BuildConfig.DEBUG)
                    Log.e(Config.TAG, "error", e);
            }
        }
    }
}

From source file:org.esupportail.nfctagdroid.NfcTacDroidActivity.java

private static String getEsupNfcTagServerUrl(Context context) {
    try {/*from  w w w .  j a  v a 2  s  .c  o  m*/
        InputStream esupnfctagPropertiesFile = context.getAssets().open("esupnfctag.properties");
        Properties props = new Properties();
        props.load(esupnfctagPropertiesFile);
        String esupNfcTagServerUrl = props.getProperty("esupNfcTagServerUrl");
        return esupNfcTagServerUrl;
    } catch (IOException e) {
        throw new NfcTagDroidException(
                "can't get esupNfcTagServerUrl property from esupnfctag.properties file !", e);
    }
}

From source file:com.theaetetuslabs.android_apkmaker.AndroidApkMaker.java

private static File unpackAsset(Context context, String assetName, File dest) throws IOException {
    if (!dest.exists()) {
        InputStream assetIn = context.getAssets().open(assetName);
        dest.createNewFile();//from   www  .  jav a  2s . c  o m
        int length = 0;
        byte[] buffer = new byte[4096];
        FileOutputStream rawOut = new FileOutputStream(dest);
        while ((length = assetIn.read(buffer)) > 0) {
            rawOut.write(buffer, 0, length);
        }
        rawOut.flush();
        rawOut.close();
        assetIn.close();
    }
    return dest;
}

From source file:org.dolphinemu.dolphinemu.utils.DirectoryInitialization.java

private static void copyAsset(String asset, File output, Boolean overwrite, Context context) {
    Log.verbose("[DirectoryInitialization] Copying File " + asset + " to " + output);

    try {/*from  w  w w  .j a  v  a2 s .c o  m*/
        if (!output.exists() || overwrite) {
            InputStream in = context.getAssets().open(asset);
            OutputStream out = new FileOutputStream(output);
            copyFile(in, out);
            in.close();
            out.close();
        }
    } catch (IOException e) {
        Log.error("[DirectoryInitialization] Failed to copy asset file: " + asset + e.getMessage());
    }
}

From source file:me.oriley.homage.Homage.java

@Nullable
private static Library[] getLibraryArray(@NonNull Context context, @NonNull String assetPath) {
    try {/*w  ww.j a  va 2  s.  c  o m*/
        InputStream stream = context.getAssets().open(assetPath);
        return parseLibraries(stream);
    } catch (IOException e) {
        Log.e(TAG, "IOException reading license file: " + assetPath, e);
        return null;
    }
}

From source file:com.android.build.gradle.internal.incremental.fixture.ClassEnhancement.java

private static Map<String, ClassLoader> setUpEnhancedClassLoaders(final ClassLoader mainClassLoader,
        final Map<String, File> compileOutputFolders, final boolean tracing) {
    return Maps.transformEntries(compileOutputFolders, new Maps.EntryTransformer<String, File, ClassLoader>() {
        @Override// ww  w . ja v a  2 s.  c o  m
        public ClassLoader transformEntry(@Nullable String patch, @Nullable File compileOutputFolder) {
            Context context = InstrumentationRegistry.getContext();
            File optimizedDir = new ContextCompat().getCodeCacheDir(context);

            try {
                InputStream is = context.getAssets().open(compileOutputFolder.getPath() + "/classes.dex");
                File output;
                try {
                    File patchDir = new File(context.getDir("patches", Context.MODE_PRIVATE), patch);
                    patchDir.mkdir();
                    output = new File(patchDir, patch + ".dex");

                    Files.asByteSink(output).writeFrom(is);
                } finally {
                    is.close();
                }

                return new DexClassLoader(output.getAbsolutePath(), optimizedDir.getAbsolutePath(), null,
                        ClassEnhancement.class.getClassLoader());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    });
}