Android从assets和raw中读取txt文件
Android从assets和raw中读取txt文件(集锦2篇)由网友“跑酷甜心”投稿提供,以下是小编给大家整理的Android从assets和raw中读取txt文件,欢迎大家前来参阅。
篇1:Android从assets和raw中读取txt文件
方法一、将要读取的txt文件拷贝到Android工程目录下的assets文件夹下
方法二、在res文件夹下新建raw文件夹,将txt拷贝到该目录下
本方法是从assets中读取
/** * 从assets中读取txt */ private void readFromAssets { try { InputStream is = getAssets().open(“qq.txt”); String text = readTextFromSDcard(is); textView.setText(text); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }本方法是从raw中读取
/** * 从raw中读取txt */ private void readFromRaw() { try { InputStream is = getResources().openRawResource(R.raw.qq); String text = readTextFromSDcard(is); textView.setText(text); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
本方法是解析输入流,返回txt中的字符串
/** * 按行读取txt * * @param is * @return * @throws Exception */ private String readTextFromSDcard(InputStream is) throws Exception { InputStreamReader reader = new InputStreamReader(is); BufferedReader bufferedReader = new BufferedReader(reader); StringBuffer buffer = new StringBuffer(“”); String str; while ((str = bufferedReader.readLine()) != null) { buffer.append(str); buffer.append(“\\n”); } return buffer.toString(); }
篇2:Android 从FrameBuffer中读取界面数据实现截图
要实现从应用层截系统屏幕的功能 , 首先你的应用需要有读取系统文件的权限 ,如何通过process = Runtime.getRuntime().exec(su); 的方式得到权限有在“Android底层事件注入,控制系统的触摸、点击、各个按钮触发”博客中提到 blog.csdn.net/fanjunjian1991/article/details/44648287 ,各位看官可前去瞧瞧,
这次截屏功能实现的方式主要读取系统FrameBuffer里的内容,利用/dev/graphics/fb0这个设备节点。【/dev/graphics/fb0代表了LCD的FrameBuffer缓冲区驱动程序,操作该驱动,即相当于操作LCD 的FrameBuffer。所以,由此出发,通过读取/dev/graphics/fb0的内容,可以很容易就得到LCD屏幕上当前正在显示的内容。】
读取文件中数据的代码如下:
byte[] piex = new byte[width * height * deepth]; InputStream stream = new FileInputStream(new File(file_name)); DataInputStream dStream = new DataInputStream(stream); dStream.readFully(piex); dStream.close(); stream.close();
然后把读到的二进制数组转换为整形数组:
int[] data = convertToColor(piex);piex = null;
public static int[] convertToColor(byte[] piex) throws Exception {switch (deepth) {case 2:return convertToColor_2byte(piex);case 3:return convertToColor_3byte(piex);case 4:return convertToColor_4byte(piex);default:throw new Exception(Deepth Error!);}}
public static int[] convertToColor_2byte(byte[] piex) { int[] colors = new int[width * height]; int len = piex.length; for (int i = 0; i < len; i += 2) { int colour = (piex[i+1] & 0xFF) << 8 | (piex[i] & 0xFF); int r = ((colour & 0xF800) >> 11)*8; int g = ((colour & 0x07E0) >> 5)*4; int b = (colour & 0x001F)*8; int a = 0xFF; colors[i / 2] = (a << 24) + (r << 16) + (g << 8) + b; } return colors; } public static int[] convertToColor_3byte(byte[] piex) { int[] colors = new int[width * height]; int len = piex.length; for (int i = 0; i < len; i += 3) { int r = (piex[i] & 0xFF); int g = (piex[i + 1] & 0xFF); int b = (piex[i + 2] & 0xFF); int a = 0xFF; colors[i / 3] = (a << 24) + (r << 16) + (g << 8) + b; } return colors; } public static int[] convertToColor_4byte(byte[] piex) { int[] colors = new int[width * height]; int len = piex.length; for (int i = 0; i < len; i += 4) { int r = (piex[i] & 0xFF); int g = (piex[i + 1] & 0xFF); int b = (piex[i + 2] & 0xFF); int a = (piex[i + 3] & 0xFF); colors[i / 4] = (a << 24) + (r << 16) + (g << 8) + b; } return colors; }
最后把整形数组转换为位图:
Bitmap bm = Bitmap.createBitmap(data, width, height, Bitmap.Config.RGB_565); data = null;