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.bhb27.isu.Main.java

public void extractAssets(String executableFilePath, String filename) {

    AssetManager assetManager = getAssets();
    InputStream inStream = null;/*from   ww w  .  ja  va  2  s  .c  om*/
    OutputStream outStream = null;

    try {

        inStream = assetManager.open(filename);
        outStream = new FileOutputStream(executableFilePath); // for override file content
        //outStream = new FileOutputStream(out,true); // for append file content

        byte[] buffer = new byte[1024];
        int length;
        while ((length = inStream.read(buffer)) > 0) {
            outStream.write(buffer, 0, length);
        }

        if (inStream != null)
            inStream.close();
        if (outStream != null)
            outStream.close();

    } catch (IOException e) {
        Log.e(TAG, "Failed to copy asset file: " + filename, e);
    }
    File execFile = new File(executableFilePath);
    execFile.setExecutable(true);
    Log.e(TAG, "Copy success: " + filename);
}

From source file:io.realm.scanner.MainActivity.java

private void copyTestAssetIfNeeded() {
    String imagePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
            + File.separator + TEST_IMAGE;
    if (new File(imagePath).exists()) {
        return;//ww w .j a v  a2s.co  m
    }

    AssetManager assetManager = getAssets();
    try {
        InputStream in = assetManager.open(TEST_IMAGE);
        OutputStream out = new FileOutputStream(imagePath);
        byte[] buffer = new byte[PRIME_NUMBER_1000th];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        in.close();
        out.flush();
        out.close();

        MediaScannerConnection.scanFile(this, new String[] { imagePath }, new String[] { "image/jpeg" }, null);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.footprint.cordova.plugin.localnotification.Options.java

/**
 * Converts an Image URI to Bitmap./*from  w  w w  .j  a v  a  2s  . c om*/
 *
 * @param src
 *      The internal image URI
 * @return
 *      The corresponding bitmap
 */
private Bitmap getIconFromURI(String src) {
    AssetManager assets = LocalNotification.context.getAssets();
    Bitmap bmp = null;

    try {
        String path = src.replace("file:/", "www");
        InputStream input = assets.open(path);

        bmp = BitmapFactory.decodeStream(input);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return bmp;
}

From source file:com.bazaarvoice.test.SubmissionTests.PhotoSubmissionTest.java

public void testPhotoSubmit3() {

    OnBazaarResponseHelper bazaarResponse = new OnBazaarResponseHelper() {
        @Override/*from w w  w. ja  va 2  s . c om*/
        public void onResponseHelper(JSONObject response) throws JSONException {
            Log.e(tag, "End of photo submit transmission : END " + System.currentTimeMillis());

            Log.i(tag, "Response = \n" + response);
            assertFalse("The test returned errors! ", response.getBoolean("HasErrors"));
            assertNotNull(response.getJSONObject("Photo"));
        }
    };

    SubmissionMediaParams mediaParams = new SubmissionMediaParams(MediaParamsContentType.REVIEW_COMMENT);
    mediaParams.setUserId(
            "735688f97b74996e214f5df79bff9e8b7573657269643d393274796630666f793026646174653d3230313130353234");

    AssetManager assets = this.mContext.getAssets();
    File dir = this.mContext.getDir("TEMP", 0);
    File file = new File(dir, "RalphRocks.jpg");

    InputStream in = null;
    FileOutputStream out = null;
    try {
        in = assets.open("RalphRocks.jpg");
        out = new FileOutputStream(file);

        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;
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        mediaParams.setPhoto(file);
    } catch (Exception e) {
        e.printStackTrace();
    }

    Log.e(tag, "Begin of photo submit transmission : BEGIN " + System.currentTimeMillis());

    submitMedia.postSubmission(RequestType.PHOTOS, mediaParams, bazaarResponse);
    bazaarResponse.waitForTestToFinish();
}

From source file:com.imagine.BaseActivity.java

Bitmap bitmapDecodeAsset(String name) {
    AssetManager assets = getAssets();
    InputStream in;//w ww.ja v a2  s. co  m
    try {
        in = assets.open(name);
    } catch (Exception e) {
        return null;
    }
    Bitmap bitmap = BitmapFactory.decodeStream(in);
    return bitmap;
}

From source file:org.chromium.ChromeI18n.java

private boolean isLocaleAvailable(String locale) throws IOException {
    List<String> availableLocales = new ArrayList<String>();
    AssetManager am = this.cordova.getActivity().getAssets();
    String[] localesArr = am.list("www/locales");
    for (String currLocale : localesArr) {
        try {//from  w  w w.j av  a2 s. c om
            // Check that the manifest.json exists
            InputStream is = am.open("www/locales/" + currLocale + "/messages.json");
            is.close();
            availableLocales.add(currLocale);
        } catch (IOException e) {
            /* Suppress not found exceptions */ }
    }
    return availableLocales.contains(locale);
}

From source file:fr.cobaltians.cobalt.Cobalt.java

private String readFileFromAssets(String file) {
    try {/*w  w  w .j  av a2 s . com*/
        AssetManager assetManager = mContext.getAssets();
        InputStream inputStream = assetManager.open(file);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder fileContent = new StringBuilder();
        int character;

        while ((character = bufferedReader.read()) != -1) {
            fileContent.append((char) character);
        }

        return fileContent.toString();
    } catch (FileNotFoundException exception) {
        if (Cobalt.DEBUG)
            Log.e(Cobalt.TAG, TAG + " - readFileFromAssets: " + file + "not found.");
    } catch (IOException exception) {
        if (Cobalt.DEBUG)
            Log.e(Cobalt.TAG, TAG + " - readFileFromAssets: IOException");
        exception.printStackTrace();
    }

    return "";
}

From source file:com.capstone.transit.trans_it.MapActivity.java

public ArrayList<String> createStopIDArrayList() {
    AssetManager mngr;
    String line = null;//  www  .  j a v  a 2s.c o m
    ArrayList<String> StopID = new ArrayList<String>();
    int count = 0;
    boolean skillcheck = false;

    try {
        mngr = getAssets();
        InputStream is = mngr.open("stops.txt");
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);

        while ((line = br.readLine()) != null) { // Read until last line in .txt file
            if (count > 0) {
                line = line.toUpperCase();
                String[] ArrayValues = line.split(","); // Seperate line by commas into a list
                StopID.add(ArrayValues[4]); // Access first index (latitude) and add to ArrayList
            }
            count++;
        }
        br.close();
    } catch (IOException e1) {
    }
    return StopID;

}

From source file:com.capstone.transit.trans_it.MapActivity.java

public ArrayList<String> createStopNameArrayList() {
    // LonV = lon values
    // Used array lists due to dyanmic size setting

    AssetManager mngr;
    String line = null;//from w ww  .j  a  v a  2s  . c o m
    ArrayList<String> NameV = new ArrayList<String>();
    int count = 0;
    boolean skillcheck = false;

    try {
        mngr = getAssets();
        InputStream is = mngr.open("stops.txt");
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);

        while ((line = br.readLine()) != null) { // Read until last line in .txt file
            if (count > 0) {
                line = line.toUpperCase();
                String[] ArrayValues = line.split(","); // Seperate line by commas into a list
                NameV.add(ArrayValues[8]); // Access first index (latitude) and add to ArrayList
            }
            count++;
        }
        br.close();
    } catch (IOException e1) {
    }
    return NameV;

}

From source file:com.capstone.transit.trans_it.MapActivity.java

public ArrayList<String> createStopCodeArrayList() {
    AssetManager mngr;
    String line = null;/* ww w.j  a v  a  2s.c  om*/
    ArrayList<String> StopCode = new ArrayList<String>();
    int count = 0;
    boolean skillcheck = false;

    try {
        mngr = getAssets();
        InputStream is = mngr.open("stops.txt");
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);

        while ((line = br.readLine()) != null) { // Read until last line in .txt file
            if (count > 0) {
                line = line.toUpperCase();
                String[] ArrayValues = line.split(","); // Seperate line by commas into a list
                StopCode.add(ArrayValues[2]); // Access first index (latitude) and add to ArrayList
            }
            count++;
        }
        br.close();
    } catch (IOException e1) {
    }
    return StopCode;

}