android 从Assets中读取中文文本
16lz
2021-01-26
package com.wo;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import org.apache.http.util.EncodingUtils;import android.app.Activity;import android.content.res.Resources;import android.os.Bundle;import android.util.Log;import android.widget.TextView;public class Dfsdf2Activity extends Activity { TextView tv; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView) this.findViewById(R.id.tv); try { String list[] = this.getAssets().list("book"); for (int i = 0; i < list.length; i++) { Log.v("tag", list[i]); } InputStream is = this.getAssets().open("book/" + list[1]); byte[] buffer = new byte[is.available()]; is.read(buffer); String newStr = EncodingUtils.getString(buffer, "gbk"); Log.v("string", newStr); tv.setText(newStr); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private String readTextFile(InputStream inputStream) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte buf[] = new byte[1024]; int len; try { while ((len = inputStream.read(buf)) != -1) { outputStream.write(buf, 0, len); } outputStream.close(); inputStream.close(); } catch (IOException e) { } return outputStream.toString(); }}
main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </ScrollView></LinearLayout>
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer