Nextrek
@ 11
Nextrek / Android / Minstrek / MinstrekLib / src / nextrek / minstrek / utils / BookUtils.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
package nextrek.minstrek.utils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import android.content.Context; import android.content.res.AssetManager; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import android.graphics.BitmapFactory.Options; import android.util.Log; public class BookUtils { private static final String TAG = "Utils"; public static Bitmap getBitmapFromAsset(Context context, String strName) { InputStream fis = null; try { fis = context.getAssets().open(strName); // context.openFileInput(strName); //return JP2.decode(fis); Options opt = new Options(); opt.inPreferredConfig = Config.RGB_565; opt.inScaled = false; return BitmapFactory.decodeStream(fis, null, opt); } catch (IOException e) { e.printStackTrace(); // ignore it } finally { try { if (fis != null) { fis.close(); } } catch (IOException e) { } } Log.e(TAG, strName + " *FAILED*"); return null; } public static String readStringFile(Context context, String filename) { InputStream fis = null; try { fis = context.getAssets().open(filename, AssetManager.ACCESS_STREAMING); // context.openFileInput(strName); return readAsString(fis); } catch (IOException e) { // ignore it, we will return null //Log.e(TAG, "readStringFile: ", e); } finally { try { if (fis != null) { fis.close(); } } catch (IOException e) { // ignore it } } return null; } public static String readAsString(InputStream in) throws IOException { StringBuilder sb = new StringBuilder(); InputStreamReader isr = new InputStreamReader(in); try { BufferedReader r = new BufferedReader(isr, 8192); try { for (String line = r.readLine(); line != null; line = r.readLine()) { sb.append(line); } return sb.toString(); } finally { if (r != null) { r.close(); r = null; } } } finally { if (isr != null) { isr.close(); isr = null; } } } } |