mc.lib.assets.AssetsHelper.java Source code

Java tutorial

Introduction

Here is the source code for mc.lib.assets.AssetsHelper.java

Source

/*
   Copyright 2012 Mikhail Chabanov
    
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
    
   http://www.apache.org/licenses/LICENSE-2.0
    
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package mc.lib.assets;

import java.io.IOException;
import java.io.InputStream;

import mc.lib.stream.StreamHelper;

import org.json.JSONObject;
import org.w3c.dom.Document;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.util.Log;

public class AssetsHelper {

    private static String LOGTAG = AssetsHelper.class.getSimpleName();

    public static String readText(Context context, String path) {
        InputStream is = null;
        try {
            is = context.getAssets().open(path);
            return StreamHelper.readStream(is);
        } catch (IOException e) {
            Log.e(LOGTAG, "Can not read asset " + path, e);
        } finally {
            StreamHelper.close(is);
        }
        return null;
    }

    public static Bitmap readImageToBitmap(Context context, String path) {
        InputStream is = null;
        try {
            is = context.getAssets().open(path);
            return BitmapFactory.decodeStream(is);
        } catch (IOException e) {
            Log.e(LOGTAG, "Can not read asset " + path, e);
        } finally {
            StreamHelper.close(is);
        }
        return null;
    }

    public static Drawable readImage(Context context, String path) {
        InputStream is = null;
        try {
            is = context.getAssets().open(path);
            return Drawable.createFromStream(is, null);
        } catch (IOException e) {
            Log.e(LOGTAG, "Can not read asset " + path, e);
        } finally {
            StreamHelper.close(is);
        }
        return null;
    }

    public static Document readXml(Context context, String filePath) {
        return null;
    }

    public static JSONObject readJson(Context context, String filePath) {
        return null;
    }
}