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:com.ibm.mobilefirst.mobileedge.interpretation.Classification.java

private String getFileAsString(Context context, String filename) {
    AssetManager assetManager = context.getAssets();
    try {/*from  w w  w .  j  a v a 2  s  .  c  o m*/
        InputStream inputStream = assetManager.open(filename);
        return getStringFromInputStream(inputStream);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:org.voidsink.anewjkuapp.utils.AppUtils.java

private static boolean importDefaultPois(Context context) {
    // import JKU Pois
    try {/*from  w  w  w  .  j  av  a  2s  .  c  o  m*/
        // write file to sd for import
        OutputStream mapFileWriter = new BufferedOutputStream(
                context.openFileOutput(DEFAULT_POI_FILE_NAME, Context.MODE_PRIVATE));
        InputStream assetData = new BufferedInputStream(context.getAssets().open(DEFAULT_POI_FILE_NAME));

        byte[] buffer = new byte[1024];
        int len = assetData.read(buffer);
        while (len != -1) {
            mapFileWriter.write(buffer, 0, len);
            len = assetData.read(buffer);
        }
        mapFileWriter.close();

        // import file
        return executeEm(context, new Callable<?>[] {
                new ImportPoiTask(context, new File(context.getFilesDir(), DEFAULT_POI_FILE_NAME), true) },
                false);
    } catch (IOException e) {
        Analytics.sendException(context, e, false);
        return false;
    }
}

From source file:com.micro.utils.F.java

/**
 * ?Assetssd?// w ww  . j a  v a2s.com
 * @param context
 * @param assetDir  "dir"
 * @param outDir    sd?
 */
public static void copyAssets2SD(Context context, String assetDir, String outDir) {
    String[] files;
    try {
        files = context.getAssets().list(assetDir);
        File outDirFile = new File(outDir);
        if (!outDirFile.exists()) {
            outDirFile.mkdirs();
        }

        for (int i = 0; i < files.length; i++) {
            String fileName = files[i];

            String[] filesChild = context.getAssets().list(fileName);
            if (filesChild != null && filesChild.length > 0) {
                copyAssets2SD(context, fileName, outDir + "/" + fileName);
            } else {
                InputStream in = null;
                if (!TextUtils.isEmpty(assetDir)) {
                    in = context.getAssets().open(assetDir + "/" + fileName);
                } else {
                    in = context.getAssets().open(fileName);
                }
                File outFile = new File(outDir + "/" + fileName);
                if (outFile.exists()) {
                    outFile.delete();
                }
                outFile.createNewFile();
                OutputStream out = new FileOutputStream(outFile);
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }

                in.close();
                out.close();

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

From source file:org.zywx.wbpalmstar.plugin.uexiconlist.utils.IconListUtils.java

public static Bitmap getImage(Context ctx, String imgUrl, JSONObject widgetJson) {
    if (imgUrl == null || imgUrl.length() == 0) {
        return null;
    }/*from  ww w. ja va 2s  .  c om*/
    Bitmap bitmap = null;
    InputStream is = null;
    imgUrl = BUtility.makeRealPath(imgUrl, widgetJson.optString(JK_WIDGET_PATH),
            widgetJson.optInt(JK_WIDGET_TYPE));
    try {
        if (imgUrl.startsWith(BUtility.F_Widget_RES_SCHEMA)) {
            is = BUtility.getInputStreamByResPath(ctx, imgUrl);
            bitmap = BitmapFactory.decodeStream(is);
        } else if (imgUrl.startsWith(BUtility.F_FILE_SCHEMA)) {
            imgUrl = imgUrl.replace(BUtility.F_FILE_SCHEMA, "");
            bitmap = BitmapFactory.decodeFile(imgUrl);
        } else if (imgUrl.startsWith(BUtility.F_Widget_RES_path)) {
            try {
                is = ctx.getAssets().open(imgUrl);
                if (is != null) {
                    bitmap = BitmapFactory.decodeStream(is);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (imgUrl.startsWith("/")) {
            bitmap = BitmapFactory.decodeFile(imgUrl);
        } else if (imgUrl.startsWith("http://")) {
            bitmap = downloadNetworkBitmap(imgUrl);
        }
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return bitmap;
}

From source file:io.github.hidroh.materialistic.FontCache.java

public Typeface get(Context context, String typefaceName) {
    if (TextUtils.isEmpty(typefaceName)) {
        return null;
    }/*  www .  java2s . c o  m*/
    if (!mTypefaceMap.containsKey(typefaceName)) {
        mTypefaceMap.put(typefaceName, Typeface.createFromAsset(context.getAssets(), typefaceName));
    }
    return mTypefaceMap.get(typefaceName);
}

From source file:org.uribeacon.beacon.UriBeaconTest.java

public void testUriBeaconTestData() throws JSONException, IOException {
    Context context = getContext();
    AssetManager am = context.getAssets();
    JSONObject testObject = inputJson(am.open("testdata.json"));
    JSONArray testData = testObject.getJSONArray("test-data");
    for (int i = 0; i < testData.length(); i++) {
        JSONObject encodingTest = testData.getJSONObject(i);

        String uri = encodingTest.getString("url");
        assertNotNull(uri);/*from   ww  w.j ava 2  s .  c o  m*/
        Integer txPowerLevel = encodingTest.optInt("tx", 20);
        Integer flags = encodingTest.optInt("flags", 0);
        UriBeacon uriBeacon;
        try {
            uriBeacon = new UriBeacon.Builder().uriString(uri).txPowerLevel(txPowerLevel.byteValue())
                    .flags(flags.byteValue()).build();
        } catch (URISyntaxException e) {
            uriBeacon = null;
        }
        JSONArray scanRecordJson = encodingTest.optJSONArray("scanRecord");
        if (scanRecordJson == null || uriBeacon == null) {
            assertNull("Null assert failed for uriBeacon", scanRecordJson);
            assertNull("Null assert failed for " + uriBeacon, uriBeacon);
        } else {
            byte[] scanRecord = jsonToByteArray(scanRecordJson);
            byte[] uriBeaconScanRecord = uriBeacon.toByteArray();
            MoreAsserts.assertEquals(scanRecord, uriBeaconScanRecord);
        }
    }
}

From source file:com.esminis.server.mariadb.server.MariaDbServerLauncher.java

private void writeToStream(OutputStream outputStream, Context context, String assetPath) throws IOException {
    writeToStream(outputStream, IOUtils.toString(context.getAssets().open(assetPath)));
}

From source file:com.blueverdi.rosietheriveter.AlbumPageFactory.java

public AlbumPageFactory(Context context, String imageSource, String audioSource) {
    this.imageSource = imageSource;
    this.audioSource = audioSource;
    this.context = context;
    try {//ww w. j a  va  2s .co m
        imageFiles = context.getAssets().list(imageSource);
    } catch (Exception e) {
        MyLog.d(TAG, e.toString());
        e.printStackTrace();
        imageFiles = new String[0];
    }
}

From source file:com.github.fountaingeyser.typefacecompat.TypefaceCompat.java

public static Typeface create(Context ctx, String familyName, int style) {
    if (isSupported(familyName)) {
        boolean styleAfterwards = false;
        String fileName = FONT_FAMILY_FILE_PREFIX.get(familyName);
        if (fileName.endsWith("-")) {
            // All styles are supported.
            fileName += STYLE_SUFFIX[style];
        } else {/*from   w w  w.  jav a 2s  . co  m*/
            switch (style) {
            case Typeface.NORMAL:
                break;
            case Typeface.BOLD:
            case Typeface.BOLD_ITALIC:
                // These styles are not supported by default. Therefore force style after retrieving normal font.
                styleAfterwards = true;
                break;
            case Typeface.ITALIC:
                fileName += STYLE_SUFFIX[style];
                break;
            }
        }
        fileName += TTF_SUFFIX;
        // Retrieve Typeface from cache.
        Typeface tf = TYPEFACE_CACHE.get(fileName);
        if (tf == null) {
            // Create Typeface and cache it for later.
            String fontPath = "fonts/" + fileName;
            tf = Typeface.createFromAsset(ctx.getAssets(), fontPath);
            if (tf != null) {
                TYPEFACE_CACHE.put(fileName, tf);
            }
        }
        if (tf != null) {
            return styleAfterwards ? Typeface.create(tf, style) : tf;
        }
    }
    // Let the default implementation of Typeface try.
    return Typeface.create(familyName, style);
}

From source file:com.axum.darivb.searchview.SlidingTabLayout.java

public SlidingTabLayout(Context context) {

    this(context, null);

    bariol_regular_tf = Typeface.createFromAsset(context.getAssets(), "fonts/Bariol_Regular.otf");
}