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:com.aimfire.gallery.cardboard.PhotoActivity.java

public Bitmap openAsset(Context context, final String filePath) {

    AssetManager assetManager = getAssets();

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = false; // No pre-scaling

    InputStream istr;/*from w  w w . j a v a  2 s  .com*/
    try {
        istr = assetManager.open(filePath);
        return BitmapFactory.decodeStream(istr);
    } catch (IOException e) {
        // handle exception
        return null;
    }
}

From source file:bizapps.com.healthforusPatient.activity.RegisterActivity.java

private void CopyAssetsbrochure(String fName) {
    AssetManager assetManager = getAssets();
    String[] files = null;/* www  .  java2s  . c  o m*/
    try {
        files = assetManager.list("");
    } catch (IOException e) {
        Log.e("tag", e.getMessage());
    }
    for (int i = 0; i < files.length; i++) {
        String fStr = files[i];
        if (fStr.equalsIgnoreCase(fName)) {
            InputStream in = null;
            OutputStream out = null;
            try {
                in = assetManager.open(files[i]);
                out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/" + files[i]);
                copyFile(in, out);
                in.close();
                in = null;
                out.flush();
                out.close();
                out = null;
                break;
            } catch (Exception e) {
                Log.e("tag", e.getMessage());
            }
        }
    }
}

From source file:com.piusvelte.taplock.client.core.TapLockSettings.java

private boolean copyFileToSDCard(String filename) {
    AssetManager assetManager = getAssets();
    String state = Environment.getExternalStorageState();
    if (!Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state))
        Toast.makeText(TapLockSettings.this, R.string.msg_sdcardunavailable, Toast.LENGTH_SHORT).show();
    else {//from w  ww .j av  a  2  s.  c  o m
        try {
            InputStream in = assetManager.open(filename);
            OutputStream out = new FileOutputStream(
                    Environment.getExternalStorageDirectory().getPath() + "/" + filename);
            byte[] buffer = new byte[1024];
            int read;
            while ((read = in.read(buffer)) != -1)
                out.write(buffer, 0, read);
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
            return true;
        } catch (IOException e) {
            Log.e(TAG, e.getMessage());
            Toast.makeText(TapLockSettings.this, R.string.msg_oops, Toast.LENGTH_SHORT).show();
        }
    }
    return false;
}

From source file:com.gnuroot.debian.GNURootMain.java

private void copyAssets(String packageName) {
    Context friendContext = null;
    try {//from www  .  j  av  a 2  s.  c o  m
        friendContext = this.createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY);
    } catch (NameNotFoundException e1) {
        return;
    }
    AssetManager assetManager = friendContext.getAssets();
    File tempFile = new File(getInstallDir().getAbsolutePath() + "/support");
    if (!tempFile.exists()) {
        tempFile.mkdir();
    }
    String[] files = null;
    try {
        files = assetManager.list("");
    } catch (IOException e) {
        Log.e("tag", "Failed to get asset file list.", e);
    }
    for (String filename : files) {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = assetManager.open(filename);
            filename = filename.replace(".mp2", "");
            filename = filename.replace(".mp3", ".tar.gz");
            File outFile = new File(tempFile, filename);
            out = new FileOutputStream(outFile);
            if (filename.contains(".tar.gz"))
                out = openFileOutput(filename, MODE_PRIVATE);
            copyFile(in, out);
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
            exec("chmod 0777 " + outFile.getAbsolutePath(), true);
        } catch (IOException e) {
            Log.e("tag", "Failed to copy asset file: " + filename, e);
        }
    }
}

From source file:com.chummy.jezebel.material.dark.activities.Main.java

private void copyAssets() {
    AssetManager assetManager = getAssets();
    String[] files = null;//from  www  .j a v a  2 s  . co  m
    try {
        files = assetManager.list("");
    } 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 {
                in = assetManager.open(filename);
                File outFile = new File(getExternalFilesDir(null), 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();
                    } catch (IOException e) {
                        Log.e("tag", "Could not close in", e);
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        Log.e("tag", "Could not close out", e);
                    }
                }
            }
        }
}

From source file:usbong.android.utils.UsbongUtils.java

public static String readTextFileInAssetsFolder(Activity a, String filename) {
    //READ A FILE
    //Reference: Jeffrey Jongko, Aug. 31, 2010
    try {//from  w ww  .j  a  v a 2  s  . com
        //          byte[] b = new byte[100];          
        AssetManager myAssetManager = a.getAssets();
        InputStream is = myAssetManager.open(filename);

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String currLineString = "";
        String finalString = "";

        while ((currLineString = br.readLine()) != null) {
            finalString = finalString + currLineString + "\n";
        }
        is.close();

        return finalString;
    } catch (Exception e) {
        System.out.println("ERROR in reading FILE in readTextFileInAssetsFolder(...).");
        e.printStackTrace();
    }
    return null;
}

From source file:org.deviceconnect.android.manager.setting.ReqResDebugActivity.java

/**
 * Http?.//from ww w .  j  a va 2s .com
 * @param method HTTP
 * @param uri ?URI
 * @param listener Http????
 */
private void executeHttpRequest(final String method, final URI uri, final HttpListener listener) {
    HttpUriRequest req = null;
    if (HttpGet.METHOD_NAME.equals(method)) {
        req = new HttpGet(uri.toString());
    } else if (HttpPost.METHOD_NAME.equals(method)) {
        req = new HttpPost(uri.toString());
    } else if (HttpPut.METHOD_NAME.equals(method)) {
        req = new HttpPut(uri.toString());
    } else if (HttpDelete.METHOD_NAME.equals(method)) {
        req = new HttpDelete(uri.toString());
    } else if ("?".equals(method)) {
        AssetManager manager = getAssets();
        try {
            // TODO ??????
            String name = "test.png";

            MultipartEntity entity = new MultipartEntity();
            InputStream in = manager.open(name);
            // ??
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int len;
            byte[] buf = new byte[4096];
            while ((len = in.read(buf)) > 0) {
                baos.write(buf, 0, len);
            }
            // ?
            entity.addPart(FileProfileConstants.PARAM_DATA, new BinaryBody(baos.toByteArray(), name));

            req = new HttpPost(uri.toString());
            ((HttpPost) req).setEntity(entity);
        } catch (UnsupportedEncodingException e) {
            return;
        } catch (IOException e) {
            return;
        }
    } else {
        return;
    }

    if (req != null) {
        executeHttpRequest(req, listener);
    }
}

From source file:org.zywx.wbpalmstar.plugin.chatkeyboard.ACEChatKeyboardView.java

/**
 * For loading smileys from assets//from  w w  w. java2s. com
 */
private Bitmap getBitmap(String path) {
    AssetManager mngr = getContext().getAssets();
    InputStream in = null;
    try {
        in = mngr.open(path);
    } catch (Exception e) {
        e.printStackTrace();
    }

    Bitmap temp = BitmapFactory.decodeStream(in, null, null);
    return temp;
}

From source file:com.softanalle.scma.MainActivity.java

private void copyTestAssets() {
    logger.debug("copyTestAssets()");
    AssetManager am = getResources().getAssets();
    String[] list;//from w  w  w  .  j a v a2 s .  c om
    try {
        list = am.list("test");
        File destDir = new File(mStorageDir);
        for (String s : list) {

            File dstFile = new File(destDir, s);

            InputStream in = am.open("test/" + s);

            FileUtils.copyInputStreamToFile(in, dstFile);

            Log.d(TAG, "Copied: " + s);
            Log.d(TAG, "Dest: " + mStorageDir);
            in.close();

        }
    } catch (IOException e) {

        e.printStackTrace();
        String message = "";
        if (e.getMessage() != null) {
            message = e.getMessage();
        }
        showError(e.toString(), message);
    } catch (Exception e) {
        String message = "";
        if (e.getMessage() != null) {
            message = e.getMessage();
        }
        showError(e.toString(), message);
    }

}

From source file:dentex.youtube.downloader.ShareActivity.java

private void downloadThumbnail(String fileUrl) {
    InputStream is = null;//from   w w w .  ja va 2 s  .  com
    URL myFileUrl = null;
    try {
        myFileUrl = new URL(fileUrl);
    } catch (MalformedURLException e) {
        try {
            myFileUrl = new URL(
                    "https://raw.github.com/dentex/ytdownloader/master/dentex.youtube.downloader/assets/placeholder.png");
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        }
        e.printStackTrace();
    }
    try {
        HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
        conn.setDoInput(true);
        conn.connect();
        is = conn.getInputStream();

        img = BitmapFactory.decodeStream(is);
    } catch (IOException e) {
        InputStream assIs = null;
        AssetManager assMan = getAssets();
        try {
            assIs = assMan.open("placeholder.png");
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        img = BitmapFactory.decodeStream(assIs);
    }
}