get Object from Asset - Android App

Android examples for App:Assets

Description

get Object from Asset

Demo Code


import android.content.Context;
import android.content.res.AssetManager;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main{
    /**/*from   w w  w .  j a v a2 s.c o m*/
     *
     * @param context context
     * @param fullPath such as "dir/file"
     * @param clazz the class what you want from gson parsing
     * @return instance of the clazz
     * @throws IOException
     */
    public static Object getObject(Context context, String fullPath,
            Class clazz) throws IOException {
        return GsonUtils.getSimpleGson().fromJson(
                new InputStreamReader(open(context, fullPath)), clazz);
    }
    /**
     * Open assets file, return inputStream
     * @param context context
     * @param fullPath such as "dir/file"
     * @return inputStream
     * @throws IOException
     */
    public static InputStream open(Context context, String fullPath)
            throws IOException {
        AssetManager assetManager = context.getAssets();
        return assetManager.open(fullPath);
    }
}

Related Tutorials