Java tutorial
//package com.java2s; import android.content.Context; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class Main { public static final String ASSETS_PREFIX = "file://android_assets/"; public static final String ASSETS_PREFIX2 = "file://android_asset/"; public static final String ASSETS_PREFIX3 = "assets://"; public static final String ASSETS_PREFIX4 = "asset://"; public static final String RAW_PREFIX = "file://android_raw/"; public static final String RAW_PREFIX2 = "raw://"; public static final String FILE_PREFIX = "file://"; public static final String DRAWABLE_PREFIX = "drawable://"; public static InputStream getStream(Context context, String url) throws IOException { String lowerUrl = url.toLowerCase(); InputStream is; if (lowerUrl.startsWith(ASSETS_PREFIX)) { String assetPath = url.substring(ASSETS_PREFIX.length()); is = getAssetsStream(context, assetPath); } else if (lowerUrl.startsWith(ASSETS_PREFIX2)) { String assetPath = url.substring(ASSETS_PREFIX2.length()); is = getAssetsStream(context, assetPath); } else if (lowerUrl.startsWith(ASSETS_PREFIX3)) { String assetPath = url.substring(ASSETS_PREFIX3.length()); is = getAssetsStream(context, assetPath); } else if (lowerUrl.startsWith(ASSETS_PREFIX4)) { String assetPath = url.substring(ASSETS_PREFIX4.length()); is = getAssetsStream(context, assetPath); } else if (lowerUrl.startsWith(RAW_PREFIX)) { String rawName = url.substring(RAW_PREFIX.length()); is = getRawStream(context, rawName); } else if (lowerUrl.startsWith(RAW_PREFIX2)) { String rawName = url.substring(RAW_PREFIX2.length()); is = getRawStream(context, rawName); } else if (lowerUrl.startsWith(FILE_PREFIX)) { String filePath = url.substring(FILE_PREFIX.length()); is = getFileStream(filePath); } else if (lowerUrl.startsWith(DRAWABLE_PREFIX)) { String drawableName = url.substring(DRAWABLE_PREFIX.length()); is = getDrawableStream(context, drawableName); } else { throw new IllegalArgumentException( String.format("Unsupported url: %s \n" + "Supported: \n%sxxx\n%sxxx\n%sxxx", url, ASSETS_PREFIX, RAW_PREFIX, FILE_PREFIX)); } return is; } private static InputStream getAssetsStream(Context context, String path) throws IOException { return context.getAssets().open(path); } private static InputStream getRawStream(Context context, String rawName) throws IOException { int id = context.getResources().getIdentifier(rawName, "raw", context.getPackageName()); if (id != 0) { try { return context.getResources().openRawResource(id); } catch (Exception e) { e.printStackTrace(); } } throw new IOException(String.format("raw of id: %s from %s not found", id, rawName)); } private static InputStream getFileStream(String path) throws IOException { return new FileInputStream(path); } private static InputStream getDrawableStream(Context context, String rawName) throws IOException { int id = context.getResources().getIdentifier(rawName, "drawable", context.getPackageName()); if (id != 0) { BitmapDrawable drawable = (BitmapDrawable) context.getResources().getDrawable(id); Bitmap bitmap = drawable.getBitmap(); ByteArrayOutputStream os = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 0, os); return new ByteArrayInputStream(os.toByteArray()); } throw new IOException(String.format("bitmap of id: %s from %s not found", id, rawName)); } }