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:ca.psiphon.ploggy.Robohash.java

private static Bitmap loadAssetToBitmap(AssetManager assetManager, String assetName) throws IOException {
    InputStream inputStream = null;
    try {//from www . j av  a2 s .  com
        inputStream = assetManager.open(new File(ASSETS_SUBDIRECTORY, assetName).getPath());
        return BitmapFactory.decodeStream(inputStream);
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
    }
}

From source file:com.facebook.TestUtils.java

public static String getAssetFileStringContents(final Context context, final String assetPath)
        throws IOException {
    InputStream inputStream = null;
    BufferedReader reader = null;
    try {//  w  w  w . ja v a2 s.  c  o  m
        final AssetManager assets = context.getResources().getAssets();
        inputStream = assets.open(assetPath);
        reader = new BufferedReader(new InputStreamReader(inputStream));
        final StringBuilder sb = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line).append("\n");
        }
        return sb.toString();
    } finally {
        Utility.closeQuietly(inputStream);
        Utility.closeQuietly(reader);
    }
}

From source file:com.rukiasoft.androidapps.comunioelpuntal.crashlogs.ExceptionHandler.java

/**
 * Look into the files folder to see if there are any "*.stacktrace" files.
 * If any are present, submit them to the trace server.
 *//*from w w w. j av  a 2  s  . c o m*/
private static void submitStackTraces() {
    try {
        Log.d(TAG, "Looking for exceptions in: " + G.FILES_PATH);
        String[] list = searchForStackTraces();
        if (list != null && list.length > 0) {
            Log.d(TAG, "Found " + list.length + " stacktrace(s)");
            for (String aList : list) {
                String filePath = G.FILES_PATH + "/" + aList;
                // Extract the version from the filename: "packagename-version-...."
                String version = aList.split("-")[0];
                Log.d(TAG, "Stacktrace in file '" + filePath + "' belongs to version " + version);
                // Read contents of stacktrace
                StringBuilder contents = new StringBuilder();
                BufferedReader input = new BufferedReader(new FileReader(filePath));
                String line;
                String androidVersion = null;
                String phoneModel = null;
                while ((line = input.readLine()) != null) {
                    if (androidVersion == null) {
                        androidVersion = line;
                        continue;
                    } else if (phoneModel == null) {
                        phoneModel = line;
                        continue;
                    }
                    contents.append(line);
                    contents.append(System.getProperty("line.separator"));
                }
                input.close();
                String stacktrace;
                stacktrace = contents.toString();
                Log.d(TAG, "Transmitting stack trace: ");
                // Transmit stack trace with POST request
                JSONObject paramsJSON = new JSONObject();
                try {
                    paramsJSON.put("packageName", G.APP_PACKAGE);
                    paramsJSON.put("packageVersion", version);
                    paramsJSON.put("phoneModel", phoneModel);
                    paramsJSON.put("androidVersion", androidVersion);
                    paramsJSON.put("stacktrace", stacktrace);
                } catch (Exception e) {
                    Log.d(TAG, "excepcin al crear el JSON");
                }

                ServerClient cliente = MainActivity.GetServerClient();
                if (cliente == null) {
                    AssetManager am = context.getAssets();
                    cliente = new ServerClient(context.getApplicationContext(), am.open("application.xml"));
                }
                cliente.connectToSendLogDeveloper(paramsJSON, aList);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.dm.material.dashboard.candybar.helpers.ReportBugsHelper.java

@Nullable
private static String buildBrokenAppFilter(Context context, File folder) {
    try {/*from   w w w  .  j  a va 2  s.  c  om*/
        AssetManager asset = context.getAssets();
        InputStream stream = asset.open("appfilter.xml");
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(stream);
        NodeList list = doc.getElementsByTagName("item");

        File fileDir = new File(folder.toString() + "/" + "broken_appfilter.xml");
        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileDir), "UTF8"));

        boolean first = true;
        for (int i = 0; i < list.getLength(); i++) {
            Node nNode = list.item(i);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;

                if (first) {
                    first = false;
                    out.append("<!-- BROKEN APPFILTER -->");
                    out.append("\n\n\n");
                }

                int drawable = context.getResources().getIdentifier(eElement.getAttribute("drawable"),
                        "drawable", context.getPackageName());
                if (drawable == 0) {
                    out.append("Activity : ").append(
                            eElement.getAttribute("component").replace("ComponentInfo{", "").replace("}", ""));
                    out.append("\n");
                    out.append("Drawable : ").append(eElement.getAttribute("drawable"));
                    out.append("\n");
                    out.append("Reason : Drawable Not Found!");
                    out.append("\n\n");
                }
            }
        }
        out.flush();
        out.close();

        return fileDir.toString();
    } catch (Exception | OutOfMemoryError e) {
        Log.d(Tag.LOG_TAG, Log.getStackTraceString(e));
    }
    return null;
}

From source file:com.mpower.mintel.android.application.MIntel.java

private static void copyAudioFiles(String[] assetName) {
    for (int i = 0; i < assetName.length; i++) {
        File file = new File(METADATA_PATH, assetName[i]);
        if (!file.exists()) {
            AssetManager mngr = getAppContext().getAssets();
            ByteArrayBuffer baf = new ByteArrayBuffer(2048);
            try {
                InputStream path = mngr.open(assetName[i]);
                BufferedInputStream bis = new BufferedInputStream(path, 1024);
                int current = 0;
                while ((current = bis.read()) != -1) {
                    baf.append((byte) current);
                }//from   w ww .j a  v a 2  s. c o  m
                byte[] bitmapdata = baf.toByteArray();

                FileOutputStream fos;
                fos = new FileOutputStream(file);
                fos.write(bitmapdata);
                fos.flush();
                fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

From source file:ee.ioc.phon.android.inimesed.MyFileUtils.java

public static void copyAssets(Context context, File baseDir) throws IOException {
    AssetManager assetManager = context.getAssets();
    String[] files = assetManager.list("hmm");

    for (String fromFile : files) {
        File toFile = new File(baseDir.getAbsolutePath() + "/" + fromFile);
        Log.i(LOG_TAG, "Copying: " + fromFile + " to " + toFile);
        InputStream in = assetManager.open("hmm/" + fromFile);
        FileUtils.copyInputStreamToFile(in, toFile);
    }//from w w  w . j  a v a2  s .  c  o m
}

From source file:eu.focusnet.app.util.ApplicationHelper.java

/**
 * Same as {@link #getProperty(String)}, but used when no asset application is ready, yet
 *
 * @param key          The key of the property to retrieve
 * @param assetManager Asset Manager/*www. j  a va  2s.  com*/
 * @return A String containing the value of the property, or {@code null} on failure.
 */
public static String getProperty(String key, AssetManager assetManager) {
    Properties properties = new Properties();
    try {
        InputStream inputStream = assetManager.open(Constant.AppConfig.ASSETS_PROPERTY_FILE);
        properties.load(inputStream);
    } catch (IOException ex) {
        throw new FocusInternalErrorException("IO error when accessing property file.");
    }
    return properties.getProperty(key);
}

From source file:Main.java

/** load the Bitmap from assets or sdcard_dir
 * @param context//from w  w w .  jav  a 2s. c om
 * @param outsideFileName
 * @param op
 * @param useSdcard
 * @param sdcard_assetdir_path
 * @return
 */
public static Bitmap loadBitmap(Context context, String outsideFileName, BitmapFactory.Options op,
        boolean useSdcard, String sdcard_assetdir_path) {
    AssetManager am = context.getAssets();

    Bitmap retval = null;
    synchronized (sCacheMap) {
        if (sCacheMap.get(outsideFileName) != null) {
            retval = sCacheMap.get(outsideFileName);
            return retval;
        }
    }

    try {
        retval = BitmapFactory.decodeStream(am.open(outsideFileName), null, op);
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (useSdcard) {
        Bitmap _tmp = BitmapFactory.decodeFile(sdcard_assetdir_path + outsideFileName, op);
        if (_tmp != null) {
            retval = _tmp;
        }
    }

    if (retval != null) {
        synchronized (sCacheMap) {
            sCacheMap.put(outsideFileName, retval);
        }

    }

    return retval;
}

From source file:com.slimroms.themecore.AssetUtils.java

public static boolean copyAsset(AssetManager assetManager, String fromAssetPath, String toPath, Cipher cipher) {
    InputStream in;/*  w  ww  .ja  va 2  s.c om*/
    File parent = new File(toPath).getParentFile();
    if (!parent.exists() && !parent.mkdirs()) {
        Log.d(TAG, "Unable to create " + parent.getAbsolutePath());
    }

    try {
        in = assetManager.open(fromAssetPath);
        if (cipher != null && fromAssetPath.endsWith(".enc")) {
            in = new CipherInputStream(in, cipher);
            if (toPath.endsWith(".enc")) {
                toPath = toPath.substring(0, toPath.lastIndexOf("."));
            }
        }
        String text = IOUtils.toString(in, Charset.defaultCharset());
        Log.d("TEST", "text=" + text);
        copyInputStreamToFile(in, new File(toPath));
        in.close();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

From source file:es.usc.citius.servando.calendula.fragments.HomeProfileMgr.java

public static Bitmap getBitmapFromAsset(Context context, String filePath) {
    AssetManager assetManager = context.getAssets();

    InputStream istr;/* w w  w.  ja  v  a 2  s.  c om*/
    Bitmap bitmap = null;
    try {
        istr = assetManager.open(filePath);
        bitmap = BitmapFactory.decodeStream(istr);
    } catch (IOException e) {
        // handle exception
    }

    return bitmap;
}