Search in sources :

Example 41 with AssetManager

use of android.content.res.AssetManager in project baker-android by bakerframework.

the class MagazineThumb method renderCover.

/**
     * Sets an image file as the cover of this instance of an issue.
     */
private void renderCover() {
    String path;
    Bitmap bmp;
    Log.d(this.getClass().getName(), "Will render cover for magazine " + this.magazine.getName());
    try {
        if (this.magazine.isStandalone()) {
            boolean fromAssets = !(this.getContext().getResources().getBoolean(R.bool.sa_read_from_custom_directory));
            if (fromAssets) {
                String books = this.getContext().getString(R.string.sa_books_directory);
                path = books.concat(File.separator).concat(this.magazine.getName()).concat(File.separator).concat(this.magazine.getCover());
                AssetManager assetManager = this.getContext().getAssets();
                bmp = BitmapFactory.decodeStream(assetManager.open(path));
            } else {
                path = Configuration.getMagazinesDirectory(this.getContext()).concat(File.separator).concat(this.magazine.getName()).concat(File.separator).concat(this.magazine.getCover());
                bmp = BitmapFactory.decodeFile(path);
            }
        } else {
            path = Configuration.getCacheDirectory(this.getContext()).concat(File.separator).concat(this.magazine.getName());
            bmp = BitmapFactory.decodeFile(path);
        }
        ((ImageView) findViewById(R.id.imgCover)).setImageBitmap(bmp);
    } catch (IOException ex) {
        Log.e(this.getClass().getName(), "Could not render cover for " + this.magazine.getName(), ex);
    }
}
Also used : Bitmap(android.graphics.Bitmap) AssetManager(android.content.res.AssetManager) ImageView(android.widget.ImageView) IOException(java.io.IOException)

Example 42 with AssetManager

use of android.content.res.AssetManager in project ARChon-Packager by bpear96.

the class activityInstalled method copyAssets.

private void copyAssets() {
    //Copy template.zip from app assets to /ChromeAPKS
    String AppNameFolder = g.getSelectedAppName();
    AssetManager assetManager = getAssets();
    String[] files = null;
    try {
        files = assetManager.list("");
    } catch (IOException e) {
        Log.e("tag", "Failed to get asset file list.", e);
    }
    assert files != null;
    for (String filename : files) {
        InputStream in;
        OutputStream out;
        try {
            in = assetManager.open(filename);
            out = new FileOutputStream(Environment.getExternalStorageDirectory().toString() + File.separator + "ChromeAPKS/" + AppNameFolder + "/" + filename);
            copyFile(in, out);
            in.close();
            out.flush();
            out.close();
        } catch (IOException e) {
            Log.e("tag", "Failed to copy asset file: " + filename, e);
        }
    }
}
Also used : AssetManager(android.content.res.AssetManager) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException)

Example 43 with AssetManager

use of android.content.res.AssetManager in project android-advancedrecyclerview by h6ah4i.

the class AssetFileLibraryLicenseDataCollector method collect.

public List<LibraryInfo> collect() {
    ArrayList<LibraryInfo> list = new ArrayList<>();
    AssetManager assets = mContext.getAssets();
    try {
        String[] dirs = assets.list(mBaseAssetDir);
        // sort numerically
        final Pattern p = Pattern.compile("^([0-9]+).*$");
        Arrays.sort(dirs, new Comparator<String>() {

            public int compare(String s1, String s2) {
                try {
                    Matcher m1 = p.matcher(s1);
                    Matcher m2 = p.matcher(s2);
                    if (m1.find() && m2.find()) {
                        int n1 = Integer.parseInt(m1.group(1));
                        int n2 = Integer.parseInt(m2.group(1));
                        if (n1 == n2) {
                            return s1.compareTo(s2);
                        }
                        return n1 - n2;
                    } else {
                        return s1.compareTo(s2);
                    }
                } catch (RuntimeException e) {
                    return s1.compareTo(s2);
                }
            }
        });
        for (String dirname : dirs) {
            LibraryInfo info = loadLicenseInfo(assets, mBaseAssetDir + "/" + dirname);
            if (info != null) {
                list.add(info);
            }
        }
    } catch (IOException e) {
        Log.e(TAG, "collect()", e);
    }
    return list;
}
Also used : Pattern(java.util.regex.Pattern) AssetManager(android.content.res.AssetManager) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 44 with AssetManager

use of android.content.res.AssetManager in project facebook-android-sdk by facebook.

the class FacebookActivityTestCase method createTempFileFromAsset.

protected File createTempFileFromAsset(String assetPath) throws IOException {
    InputStream inputStream = null;
    FileOutputStream outStream = null;
    try {
        AssetManager assets = getActivity().getResources().getAssets();
        inputStream = assets.open(assetPath);
        // context being the Activity pointer
        File outputDir = getActivity().getCacheDir();
        File outputFile = File.createTempFile("prefix", assetPath, outputDir);
        outStream = new FileOutputStream(outputFile);
        final int bufferSize = 1024 * 2;
        byte[] buffer = new byte[bufferSize];
        int n = 0;
        while ((n = inputStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, n);
        }
        return outputFile;
    } finally {
        Utility.closeQuietly(outStream);
        Utility.closeQuietly(inputStream);
    }
}
Also used : AssetManager(android.content.res.AssetManager) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 45 with AssetManager

use of android.content.res.AssetManager in project facebook-android-sdk by facebook.

the class FacebookActivityTestCase method readApplicationIdAndSecret.

protected synchronized void readApplicationIdAndSecret() {
    synchronized (FacebookTestCase.class) {
        if (applicationId != null && applicationSecret != null && clientToken != null) {
            return;
        }
        AssetManager assets = getInstrumentation().getTargetContext().getResources().getAssets();
        InputStream stream = null;
        final String errorMessage = "could not read applicationId and applicationSecret from config.json; ensure " + "you have run 'configure_unit_tests.sh'. Error: ";
        try {
            stream = assets.open("config.json");
            String string = Utility.readStreamToString(stream);
            JSONTokener tokener = new JSONTokener(string);
            Object obj = tokener.nextValue();
            if (!(obj instanceof JSONObject)) {
                fail(errorMessage + "could not deserialize a JSONObject");
            }
            JSONObject jsonObject = (JSONObject) obj;
            applicationId = jsonObject.optString("applicationId");
            applicationSecret = jsonObject.optString("applicationSecret");
            clientToken = jsonObject.optString("clientToken");
            if (Utility.isNullOrEmpty(applicationId) || Utility.isNullOrEmpty(applicationSecret) || Utility.isNullOrEmpty(clientToken)) {
                fail(errorMessage + "config values are missing");
            }
        } catch (IOException e) {
            fail(errorMessage + e.toString());
        } catch (JSONException e) {
            fail(errorMessage + e.toString());
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e) {
                    fail(errorMessage + e.toString());
                }
            }
        }
    }
}
Also used : JSONTokener(org.json.JSONTokener) AssetManager(android.content.res.AssetManager) JSONObject(org.json.JSONObject) InputStream(java.io.InputStream) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) IOException(java.io.IOException)

Aggregations

AssetManager (android.content.res.AssetManager)196 IOException (java.io.IOException)70 InputStream (java.io.InputStream)58 Resources (android.content.res.Resources)47 XmlResourceParser (android.content.res.XmlResourceParser)28 File (java.io.File)28 DisplayMetrics (android.util.DisplayMetrics)24 Configuration (android.content.res.Configuration)15 BufferedReader (java.io.BufferedReader)13 FileOutputStream (java.io.FileOutputStream)13 Bitmap (android.graphics.Bitmap)12 InputStreamReader (java.io.InputStreamReader)12 FileInputStream (java.io.FileInputStream)10 FileNotFoundException (java.io.FileNotFoundException)10 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)10 AndroidRuntimeException (android.util.AndroidRuntimeException)8 OutputStream (java.io.OutputStream)8 Method (java.lang.reflect.Method)8 Intent (android.content.Intent)7 AttributeSet (android.util.AttributeSet)7