Example usage for android.content.res AssetManager open

List of usage examples for android.content.res AssetManager open

Introduction

In this page you can find the example usage for android.content.res AssetManager open.

Prototype

public @NonNull InputStream open(@NonNull String fileName) throws IOException 

Source Link

Document

Open an asset using ACCESS_STREAMING mode.

Usage

From source file:org.zywx.wbpalmstar.engine.EBrowser.java

protected Bitmap getImage(String bgPath) {
    if (null == bgPath) {
        return null;
    }/*  w w  w .j  a v  a 2s  .co  m*/
    if (null == mBgBitmapCache) {
        mBgBitmapCache = new Hashtable<String, Bitmap>();
    }
    Bitmap result = mBgBitmapCache.get(bgPath);
    if (null != result) {
        return result;
    }
    InputStream in = null;
    try {
        if (null != bgPath && 0 != bgPath.length()) {
            if (bgPath.startsWith("/sdcard")) {
                File file = new File(bgPath);
                in = new FileInputStream(file);
            } else if (bgPath.startsWith("widget/")) {
                AssetManager asm = mContext.getAssets();
                in = asm.open(bgPath);
            } else if (bgPath.startsWith("/data/data")) {
                File file = new File(bgPath);
                in = new FileInputStream(file);
            } else {
                File file = new File(bgPath);
                in = new FileInputStream(file);
            }
            if (null != in) {
                result = BitmapFactory.decodeStream(in);
                in.close();
                mBgBitmapCache.put(bgPath, result);
                return result;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (null != in) {
            try {
                in.close();
            } catch (Exception e) {
                ;
            }
        }
    }
    return null;
}

From source file:com.gnuroot.rsinstaller.RSLauncherMain.java

/**
 * Renames assets from .mp3 to .tar.gz./*from w w w  .  j av a  2  s  .  c o  m*/
 * @param packageName
  */
private void copyAssets(String packageName) {
    Context friendContext = null;
    try {
        friendContext = this.createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY);
    } catch (NameNotFoundException e1) {
        return;
    }
    AssetManager assetManager = friendContext.getAssets();
    String[] files = null;
    try {
        files = assetManager.list("");
    } catch (IOException e) {
        Log.e("tag", "Failed to get asset file list.", e);
    }
    for (String filename : files) {
        Log.i("files ", filename);
        InputStream in = null;
        OutputStream out = null;
        try {
            in = assetManager.open(filename);
            filename = filename.replace(".mp3", ".tar.gz");
            out = openFileOutput(filename, MODE_PRIVATE);
            copyFile(in, out);
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
        } catch (IOException e) {
            Log.e("tag", "Failed to copy asset file: " + filename, e);
        }
    }
}

From source file:com.max2idea.android.fwknop.Fwknop.java

private void installNativeLib(String lib, String destDir) {
    if (true) {//from ww  w. j a  v  a  2  s.  c  o m
        try {
            String libLocation = destDir + "/" + lib;
            AssetManager am = this.getAssets();
            InputStream is = am.open(lib);
            OutputStream os = new FileOutputStream(libLocation);
            byte[] buf = new byte[8092];
            int n;
            while ((n = is.read(buf)) > 0) {
                os.write(buf, 0, n);
            }
            os.close();
            is.close();
        } catch (Exception ex) {
            Log.e("JNIExample", "failed to install native library: " + ex);
        }
    }

}

From source file:com.google.blockly.android.BlocklyActivityHelper.java

/**
 * Reloads the toolbox from assets.// ww  w. j  a  v a  2  s  . c  om
 * @param toolboxContentsXmlPath The asset path to the toolbox XML
 * @throws IllegalStateException If error occurs during loading.
 */
public void reloadToolbox(String toolboxContentsXmlPath) {
    AssetManager assetManager = mActivity.getAssets();
    BlocklyController controller = getController();
    try {
        controller.loadToolboxContents(assetManager.open(toolboxContentsXmlPath));
    } catch (IOException | BlockLoadingException e) {
        // compile time assets such as assets are assumed to be good.
        throw new IllegalStateException("Failed to load toolbox XML.", e);
    }
}

From source file:com.photon.phresco.nativeapp.eshop.activity.PhrescoActivity.java

/**
 * Read phresco-env-config.xml file to get to connect to web service
 *///  w w  w.j  a  va2s .  c om
public void readConfigXML() {
    try {

        String protocol = "protocol";
        String host = "host";
        String port = "port";
        String context = "context";
        Resources resources = getResources();
        AssetManager assetManager = resources.getAssets();

        // Read from the /assets directory
        InputStream inputStream = assetManager.open(Constants.PHRESCO_ENV_CONFIG);

        ConfigReader confReaderObj = new ConfigReader(inputStream);

        PhrescoLogger.info(TAG + "Default ENV = " + confReaderObj.getDefaultEnvName());

        List<Configuration> configByEnv = confReaderObj.getConfigByEnv(confReaderObj.getDefaultEnvName());

        for (Configuration configuration : configByEnv) {
            properties = configuration.getProperties();
            PhrescoLogger.info(TAG + "config value = " + configuration.getProperties());
            String webServiceProtocol = properties.getProperty(protocol).endsWith("://")
                    ? properties.getProperty(protocol)
                    : properties.getProperty(protocol) + "://"; // http://

            String webServiceHost = properties.getProperty(port).equalsIgnoreCase("")
                    ? (properties.getProperty(host).endsWith("/") ? properties.getProperty(host)
                            : properties.getProperty(host) + "/")
                    : properties.getProperty(host); // localhost/
            // localhost

            String webServicePort = properties.getProperty(port).equalsIgnoreCase("") ? ""
                    : (properties.getProperty(port).startsWith(":") ? properties.getProperty(port)
                            : ":" + properties.getProperty(port)); // "" (blank)
            // :1313

            String webServiceContext = properties.getProperty(context).startsWith("/")
                    ? properties.getProperty(context)
                    : "/" + properties.getProperty(context); // /phresco

            Constants.setWebContextURL(
                    webServiceProtocol + webServiceHost + webServicePort + webServiceContext + "/");
            Constants.setRestAPI(Constants.REST_API);
            PhrescoLogger.info(
                    TAG + "Constants.webContextURL : " + Constants.getWebContextURL() + Constants.getRestAPI());
        }

    } catch (ParserConfigurationException ex) {
        PhrescoLogger.info(TAG + "readConfigXML : ParserConfigurationException: " + ex.toString());
        PhrescoLogger.warning(ex);
    } catch (SAXException ex) {
        PhrescoLogger.info(TAG + "readConfigXML : SAXException: " + ex.toString());
        PhrescoLogger.warning(ex);
    } catch (IOException ex) {
        PhrescoLogger.info(TAG + "readConfigXML : IOException: " + ex.toString());
        PhrescoLogger.warning(ex);
    } catch (Exception ex) {
        PhrescoLogger.info(TAG + "readConfigXML : Exception: " + ex.toString());
        PhrescoLogger.warning(ex);
    }
}

From source file:com.mousebird.maply.MaplyStarModel.java

public MaplyStarModel(String fileName, String imageName, Activity activity) throws IOException {
    AssetManager assetMgr = activity.getAssets();
    InputStream inputStream = null;
    String[] paths = assetMgr.list("maplystarmodel");
    for (String path : paths) {
        if (path.equals(imageName)) {
            //image
            BufferedInputStream bufferedInputStream = null;
            try {
                inputStream = assetMgr.open("maplystarmodel/" + path);
                bufferedInputStream = new BufferedInputStream(inputStream);
                image = BitmapFactory.decodeStream(bufferedInputStream);
            } finally {
                if (bufferedInputStream != null) {
                    try {
                        bufferedInputStream.close();
                    } catch (IOException e) {
                    }//  ww  w.ja va  2s  . com
                }
            }
        }
        if (path.equals(fileName)) {
            //data
            Matcher m;
            try {
                inputStream = assetMgr.open("maplystarmodel/" + path);
                String stars = IOUtils.toString(inputStream, Charset.defaultCharset());
                Pattern p = Pattern.compile("[-]?[0-9]*\\.?[0-9]+");
                m = p.matcher(stars);
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                    }
                }
            }

            this.stars = new ArrayList<SingleStar>();
            if (m.groupCount() % 3 == 0) {
                int i = 0;
                SingleStar s = null;
                while (m.find()) {
                    switch (i) {
                    case 0:
                        s = new SingleStar();
                        s.ra = Float.valueOf(m.group());
                        i++;
                        break;
                    case 1:
                        s.dec = Float.valueOf(m.group());
                        i++;
                        break;
                    case 2:
                        s.mag = Float.valueOf(m.group());
                        this.stars.add(s);
                        i = 0;
                        break;
                    default:
                        break;
                    }
                }
            }
        }
    }
}

From source file:com.orange.ocara.model.RuleSetLoaderImpl.java

private void PictosFolder(AssetManager assetManager) throws IOException {
    String[] assetsList = assetManager.list(PICTOS_FOLDER);

    for (String asset : assetsList) {
        if (isPngFile(asset) | isJpgFile(asset) | isGifFile(asset)) {
            Timber.i("pictos to install : %s", asset);

            File rulesetDirPicto = installRuleset(PICTOS_FOLDER);
            File target = new File(rulesetDirPicto, asset);

            FileUtils.copyInputStreamToFile(assetManager.open(PICTOS_FOLDER + "/" + asset), target);
        }// w  ww.j  a v  a2 s. c o  m
    }
}

From source file:com.orange.ocara.model.RuleSetLoaderImpl.java

private void imagesFolder(AssetManager assetManager) throws IOException {
    String[] assetsList = assetManager.list(IMAGES_FOLDER);

    for (String asset : assetsList) {
        if (isPngFile(asset) | isJpgFile(asset) | isGifFile(asset)) {
            Timber.i("images to install : %s", asset);

            File rulesetDirImage = installRuleset(IMAGES_FOLDER);
            File target = new File(rulesetDirImage, asset);

            FileUtils.copyInputStreamToFile(assetManager.open(IMAGES_FOLDER + "/" + asset), target);
        }//  www  . j a  va 2  s .  co  m
    }
}

From source file:de.dhbw.advisory.recipe.RecipeFragment.java

@Override
public void onAttach(Context context) {
    Log.i("Recipe-Fragment - onAttach", "Methode gestartet");
    super.onAttach(context);
    //Der Developer key wird spter aus der properties-Datei ausgelesen
    Properties properties = new Properties();

    try {//from w ww  .j  a v  a2 s  . c  om
        //Hier wird der API-Key geladen
        AssetManager assetManager = context.getAssets();
        InputStream inputStream = assetManager.open("apikey.properties");
        properties.load(inputStream);
        apiKey = properties.getProperty("spoonacular.apikey");
    } catch (IOException e) {
        //Falls das Fragment nicht geladen werden konnte
        Toast.makeText(context, "Fragment konnte nicht geladen werden", Toast.LENGTH_SHORT).show();
    }
    Log.i("Recipe-Fragment - onAttach", "Methode beendet");
}

From source file:com.orange.ocara.model.RuleSetLoaderImpl.java

@Override
public void initialize() {

    final AssetManager assetManager = context.getAssets();

    try {/*from   www. j a  v a2  s.c om*/
        String[] assetsList = assetManager.list(RULESETS_FOLDER);

        for (String asset : assetsList) {
            if (isJsonFile(asset)) {
                Timber.i("ruleset to install : %s", asset);

                File rulesetDir = installRuleset(asset);
                File target = new File(rulesetDir, asset);

                FileUtils.copyInputStreamToFile(assetManager.open(RULESETS_FOLDER + "/" + asset), target);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        PictosFolder(assetManager);

    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        imagesFolder(assetManager);

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

}