Asset文件操作工具箱
来源:互联网 发布:怎么区分淘宝二手镜头 编辑:程序博客网 时间:2024/06/18 09:07
/* * Copyright (C) 2013 Peng fei Pan <sky@xiaopan.me> * * 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 me.xiaopan.android.content.res;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Rect;import java.io.IOException;import java.io.InputStream;import java.nio.charset.Charset;import me.xiaopan.android.graphics.BitmapDecoder;import me.xiaopan.java.io.IOUtils;/** * Asset文件操作工具箱 */public class AssetUtils { /** * 读取给定文件名的文件的内容并转换成字符串 * @param context 上下文 * @param fileName 文件名 * @param charset 转换编码 * @return 读取结果 */ public static String getString(Context context, String fileName, Charset charset){ InputStream inputStream = null; try { inputStream = context.getAssets().open(fileName); byte[] bytes = IOUtils.read(inputStream); inputStream.close(); return new String(bytes, 0, bytes.length, charset.name()); } catch (IOException e) { e.printStackTrace(); if(inputStream != null){ try { inputStream.close(); } catch (IOException e1) { e1.printStackTrace(); } } return null; } } /** * 读取给定文件名的文件的内容并转换成字符串 * @param context 上下文 * @param fileName 文件名 * @return 读取结果 */ public static String getString(Context context, String fileName){ InputStream inputStream = null; try { inputStream = context.getAssets().open(fileName); byte[] bytes = IOUtils.read(inputStream); inputStream.close(); return new String(bytes, 0, bytes.length, Charset.defaultCharset().name()); } catch (IOException e) { e.printStackTrace(); if(inputStream != null){ try { inputStream.close(); } catch (IOException e1) { e1.printStackTrace(); } } return null; } } /** * 获取位图 * @param context 上下文 * @param fileName 文件名称 * @param outPadding 输出位图的内边距 * @param options 加载选项 * @return Bitmap */ public static Bitmap getBitmap(Context context, String fileName, Rect outPadding, BitmapFactory.Options options){ return new BitmapDecoder().decodeFromAssets(context, fileName, outPadding, options); } /** * 获取位图 * @param context 上下文 * @param fileName 文件名称 * @return Bitmap */ public static Bitmap getBitmap(Context context, String fileName){ return new BitmapDecoder().decodeFromAssets(context, fileName); }}
0 0
- Asset文件操作工具箱
- Java工具箱之常见处理文件操作
- 矩形操作工具箱
- 关于访问asset子目录文件
- 从asset中读取文件
- Asset资源文件的使用方法
- asset
- asset
- android 对于asset下文件的操作
- 加载asset中的文件字体以及使用
- 读取Rescource和asset文件夹中的文件
- asset 和raw放文件注意事项
- 有关asset pipeline与stylesheets文件位置
- asset中获取文件并读取数据
- 读取Asset文件夹下的Json文件
- AssetManager查看asset文件中的图片
- android 读取asset文件方法封装
- 读取asset文件的几种方式
- JDBC(Java Database Connection) 教程
- 模拟ReadLine
- ls查看文件大小
- Exception in thread “main” javax.xml.bind.JAXBException: Provider com.sun.xml.bind.v2.ContextFactory
- CVPR 2015 Oral概览 - 第三天上午
- Asset文件操作工具箱
- mysql主从复制Error_code: 1872
- Object-C宏定义与const变量
- Storm Trident
- 线性函数拟合R语言示例
- linux图形化界面更改网络配置
- Thinkphp的学习——U函数的使用
- 在线LCA倍增法
- csipsimple结构浅析