get Resources - Android App

Android examples for App:Resource

Description

get Resources

Demo Code


//package com.java2s;
import java.io.File;
import java.lang.ref.WeakReference;

import java.util.HashMap;
import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;

public class Main {
    private static final HashMap<String, WeakReference<Resources>> resources = new HashMap<String, WeakReference<Resources>>();

    public static Resources getResources(Context ctx, File file)
            throws Exception {
        String path = file.getAbsolutePath();
        WeakReference<Resources> wr = resources.get(path);
        if (wr != null) {
            Resources res = wr.get();
            if (res != null) {
                return res;
            }//from www  .java2  s .c o m
        }

        AssetManager am = (AssetManager) AssetManager.class.newInstance();
        am.getClass().getMethod("addAssetPath", String.class)
                .invoke(am, file.getAbsolutePath());

        Resources superRes = ctx.getResources();
        Resources res = new Resources(am, superRes.getDisplayMetrics(),
                superRes.getConfiguration());

        resources.put(path, new WeakReference<Resources>(res));
        return res;
    }
}

Related Tutorials