Search in sources :

Example 71 with AssetManager

use of android.content.res.AssetManager in project BlogSource by TeachCourse.

the class MainActivity method getFromAssetsPath.

/**
 * 读取assets指定的xml文件,返回List
 * @param fileName 文件名称
 * @return List
 */
public List getFromAssetsPath(String fileName) {
    InputStream is = null;
    AssetManager manager = getAssets();
    try {
        is = manager.open(fileName);
        List list = NewsService.getNewsBean(is);
        Message msg = new Message();
        msg.obj = list;
        msg.what = 0x110;
        handler.sendMessage(msg);
        return list;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (is != null)
                is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}
Also used : AssetManager(android.content.res.AssetManager) Message(android.os.Message) InputStream(java.io.InputStream) List(java.util.List) IOException(java.io.IOException) IOException(java.io.IOException)

Example 72 with AssetManager

use of android.content.res.AssetManager in project MifareClassicTool by ikarus23.

the class MainMenu method copyStdKeysFilesIfNecessary.

/**
 * Copy the standard key files ({@link Common#STD_KEYS} and
 * {@link Common#STD_KEYS_EXTENDED}) form assets to {@link Common#KEYS_DIR}.
 * Key files are simple text files. Any plain text editor will do the trick.
 * All key and dump data from this App is stored in
 * getExternalStoragePublicDirectory(Common.HOME_DIR) to remain
 * there after App uninstallation.
 * @see Common#KEYS_DIR
 * @see Common#HOME_DIR
 * @see Common#copyFile(InputStream, OutputStream)
 */
private void copyStdKeysFilesIfNecessary() {
    File std = Common.getFileFromStorage(Common.HOME_DIR + "/" + Common.KEYS_DIR + "/" + Common.STD_KEYS);
    File extended = Common.getFileFromStorage(Common.HOME_DIR + "/" + Common.KEYS_DIR + "/" + Common.STD_KEYS_EXTENDED);
    AssetManager assetManager = getAssets();
    if (!std.exists()) {
        // Copy std.keys.
        try {
            InputStream in = assetManager.open(Common.KEYS_DIR + "/" + Common.STD_KEYS);
            OutputStream out = new FileOutputStream(std);
            Common.copyFile(in, out);
            in.close();
            out.flush();
            out.close();
        } catch (IOException e) {
            Log.e(LOG_TAG, "Error while copying 'std.keys' from assets " + "to external storage.");
        }
    }
    if (!extended.exists()) {
        // Copy extended-std.keys.
        try {
            InputStream in = assetManager.open(Common.KEYS_DIR + "/" + Common.STD_KEYS_EXTENDED);
            OutputStream out = new FileOutputStream(extended);
            Common.copyFile(in, out);
            in.close();
            out.flush();
            out.close();
        } catch (IOException e) {
            Log.e(LOG_TAG, "Error while copying 'extended-std.keys' " + "from assets to external storage.");
        }
    }
}
Also used : AssetManager(android.content.res.AssetManager) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 73 with AssetManager

use of android.content.res.AssetManager in project devbricks by dailystudio.

the class BitmapUtils method loadAssetBitmap.

public static Bitmap loadAssetBitmap(Context context, String assetFile) {
    AssetManager assetManager = context.getAssets();
    if (assetManager == null) {
        return null;
    }
    if (TextUtils.isEmpty(assetFile)) {
        return null;
    }
    InputStream istream = null;
    Bitmap bitmap = null;
    try {
        istream = assetManager.open(assetFile);
        if (istream != null) {
            bitmap = BitmapFactory.decodeStream(istream);
        }
    } catch (OutOfMemoryError e) {
        Logger.warn("could not decode asset bitmap: %s", assetFile, e.toString());
        bitmap = null;
    } catch (IOException e) {
        Logger.warn("could not decode asset bitmap: %s", assetFile, e.toString());
        bitmap = null;
    } finally {
        try {
            if (istream != null) {
                istream.close();
            }
        } catch (IOException e) {
        }
    }
    return bitmap;
}
Also used : Bitmap(android.graphics.Bitmap) AssetManager(android.content.res.AssetManager) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 74 with AssetManager

use of android.content.res.AssetManager in project devbricks by dailystudio.

the class FileUtils method copyAssetFile.

public static boolean copyAssetFile(Context context, String assetFile, String dstFile) throws IOException {
    if (context == null || assetFile == null || dstFile == null) {
        return false;
    }
    final AssetManager asstmgr = context.getAssets();
    if (asstmgr == null) {
        return false;
    }
    InputStream istream = asstmgr.open(assetFile);
    if (istream == null) {
        return false;
    }
    FileOutputStream ostream = new FileOutputStream(dstFile);
    return ResourcesUtils.copyToFile(istream, ostream);
}
Also used : AssetManager(android.content.res.AssetManager) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream)

Example 75 with AssetManager

use of android.content.res.AssetManager in project devbricks by dailystudio.

the class FileUtils method getAssetFileContent.

public static String getAssetFileContent(Context context, String file) throws IOException {
    if (context == null || file == null) {
        return null;
    }
    final AssetManager asstmgr = context.getAssets();
    if (asstmgr == null) {
        return null;
    }
    final String encoding = detectFileEncoding(asstmgr.open(file));
    Logger.debug("encoding = %s", encoding);
    InputStream istream = asstmgr.open(file);
    if (istream == null) {
        return null;
    }
    return getFileContent(istream, encoding);
}
Also used : AssetManager(android.content.res.AssetManager) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream)

Aggregations

AssetManager (android.content.res.AssetManager)346 IOException (java.io.IOException)141 InputStream (java.io.InputStream)121 Resources (android.content.res.Resources)75 File (java.io.File)54 FileOutputStream (java.io.FileOutputStream)34 XmlResourceParser (android.content.res.XmlResourceParser)32 DisplayMetrics (android.util.DisplayMetrics)31 Bitmap (android.graphics.Bitmap)23 Configuration (android.content.res.Configuration)21 BufferedReader (java.io.BufferedReader)21 InputStreamReader (java.io.InputStreamReader)20 ArrayList (java.util.ArrayList)20 FileInputStream (java.io.FileInputStream)18 OutputStream (java.io.OutputStream)17 Context (android.content.Context)16 Intent (android.content.Intent)16 JsonParser (com.google.gson.JsonParser)16 ByteArrayInputStream (java.io.ByteArrayInputStream)16 Method (java.lang.reflect.Method)16