android 工具类篇 ResourceUtils

本文介绍了一个用于Android应用中读取资源文件的工具类ResourceUtils。该工具类提供了从assets和raw目录中读取文件内容的方法,支持返回字符串或列表形式的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;

/**
 * ResourceUtils
 * 
 * @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2012-5-26
 */
public class ResourceUtils {

    /**
     * get an asset using ACCESS_STREAMING mode. This provides access to files that have been bundled with an
     * application as assets -- that is, files placed in to the "assets" directory.
     * 
     * @param context
     * @param fileName The name of the asset to open. This name can be hierarchical.
     * @return
     */
    public static String geFileFromAssets(Context context, String fileName) {
        if (context == null || StringUtils.isEmpty(fileName)) {
            return null;
        }

        StringBuilder s = new StringBuilder("");
        try {
            InputStreamReader in = new InputStreamReader(context.getResources().getAssets().open(fileName));
            BufferedReader br = new BufferedReader(in);
            String line;
            while ((line = br.readLine()) != null) {
                s.append(line);
            }
            return s.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * get content from a raw resource. This can only be used with resources whose value is the name of an asset files
     * -- that is, it can be used to open drawable, sound, and raw resources; it will fail on string and color
     * resources.
     * 
     * @param context
     * @param resId The resource identifier to open, as generated by the appt tool.
     * @return
     */
    public static String geFileFromRaw(Context context, int resId) {
        if (context == null) {
            return null;
        }

        StringBuilder s = new StringBuilder();
        try {
            InputStreamReader in = new InputStreamReader(context.getResources().openRawResource(resId));
            BufferedReader br = new BufferedReader(in);
            String line;
            while ((line = br.readLine()) != null) {
                s.append(line);
            }
            return s.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * same to {@link ResourceUtils#geFileFromAssets(Context, String)}, but return type is List<String>
     * 
     * @param context
     * @param fileName
     * @return
     */
    public static List<String> geFileToListFromAssets(Context context, String fileName) {
        if (context == null || StringUtils.isEmpty(fileName)) {
            return null;
        }

        List<String> fileContent = new ArrayList<String>();
        try {
            InputStreamReader in = new InputStreamReader(context.getResources().getAssets().open(fileName));
            BufferedReader br = new BufferedReader(in);
            String line;
            while ((line = br.readLine()) != null) {
                fileContent.add(line);
            }
            br.close();
            return fileContent;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * same to {@link ResourceUtils#geFileFromRaw(Context, int)}, but return type is List<String>
     * 
     * @param context
     * @param resId
     * @return
     */
    public static List<String> geFileToListFromRaw(Context context, int resId) {
        if (context == null) {
            return null;
        }

        List<String> fileContent = new ArrayList<String>();
        BufferedReader reader = null;
        try {
            InputStreamReader in = new InputStreamReader(context.getResources().openRawResource(resId));
            reader = new BufferedReader(in);
            String line = null;
            while ((line = reader.readLine()) != null) {
                fileContent.add(line);
            }
            reader.close();
            return fileContent;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

一个简单的开源Android工具类库,提供许多常用的类帮助我们开发程序。 AndroidCommon 一个简单的开源Android工具类库,提供许多常用的类帮助我们开发程序。 These are the Android Common Utils. Class Introduction AnimationUtils Animation Unility Class AppUtils App Unility Class ArrayUtils Array Unility Class AssetsUtils Assets Unility Class BASE64Utils Base64 Unility Class BitmapUtils Bitmap Unility Class BlurUtils Blur Unility Class ByteUtils Byte Unility Class CalendarUtils Calendar Unility Class ClipboardUtils Clipboard Unility Class CollectionUtils Collection Unility Class CommonUtils Common Unility Class CpuUtils Cpu Unility Class DeviceUtils Device Unility Class DisplayUtils Display Unility Class FileUtils File Unility Class FragmentUtils Fragment Unility Class HandlerUtils Handler Unility Class IOUtils IO Unility Class ImageUtils Image Unility Class InputMethodUtils InputMethod Unility Class IntentUtils Intent Unility Class JsonUtils Json Unility Class LogUtils Log Unility Class MD5Utils Md5 Unility Class MapUtils Map Unility Class MemoryUtils Memory Unility Class NetworkUtils Network Unility Class NumberUtils Number Unility Class ObjectUtils Object Unility Class PackageUtils Package Unility Class PropertyUtils Property Unility Class RandomUtils Random Unility Class ResourceUtils Resource Unility Class SHA1Utils Sha1 Unility Class SerializableUtils Serializable Unility Class SharedPreferencesUtils SharedPreferences Unility Class ShellUtils Shell Unility Class StringUtils String Unility Class SystemUtils System Unility Class TelephonyUtils Telephony Unility Class ThreadUtils Thread Unility Class TimeUtils Time Unility Class ToastUtils Toast Unility Class UrlUtils Url Unility Class VibratorUtils Vibrator Unility Class ViewUtils View Unility Class Permission <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> Proguard -keep class com.wx.android.common.** { *; } -keepclassmembers class com.wx.android.common.** { *; } -dontwarn com.wx.android.common.** Setup Download the project from GitHub Import it to your Eclipse workspace or IntelliJ IDEA project Set your project properties, then add a android project library, and select AndroidCommon Usage Gradle: compile 'com.wx.android.common:common:1.0.1' Author venshine venshine.cn@gmail.com License Copyright (C) 2015 venshine.cn@gmail.com 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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值