Android 从SD卡上读取文本信息
2013-12-08 14:06
477 查看
从SD卡上读取文本信息
代码实现:
String fileName = "/sdcard/y.txt";//文件路径
// 也可以用String fileName = "mnt/sdcard/Y.txt";
String res = "";
try {
FileInputStream fin = new FileInputStream(fileName);
// FileInputStream fin = openFileInput(fileName);
// 用这个就不行了,必须用FileInputStream
int length = fin.available();
byte[] buffer = new byte[length];
fin.read(buffer);
res = EncodingUtils.getString(buffer, "GBK");//依Y.txt的编码类型选择合适的编码,如果不调整会乱码
fin.close();//关闭资源
System.out.println("res--->"+res);
int a=Integer.parseInt(res.substring(3, 5));
int b=Integer.parseInt(res.substring(8, 10));
System.out.println(a+"res--->"+b);//获取的a.b
} catch (Exception e) {
e.printStackTrace();
}
res = EncodingUtils.getString(buffer, "GBK");
这儿charset="GBK"
charset还可以等于"UTF-8"、"BIG-5"
从SD卡上读取图片信息(这儿读取已知路径下的jpeg格式的图片)
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imageview);
ImageView imageView=(ImageView)findViewById(R.id.imageView);
String location="";
if (isSdcard() != null) {
File file = new File(isSdcard());
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].getName().equals("13.jpeg")) {
Bitmap bitmap = BitmapFactory.decodeFile(files[i].getName());
location = isSdcard() + "/" + files[i].getName();
imageView.setImageDrawable(Drawable.createFromPath(location));
System.out.println("13.jpeg的位置:"+location);
}
}
}
}
private String isSdcard() {
File sdcardDir = null;
boolean isSDExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
// 判断SD卡是否存在
if (isSDExist) {
// 如果存在SDcard 就找到跟目录
sdcardDir = Environment.getExternalStorageDirectory();
return sdcardDir.toString();
} else {
return null;
}
}
}
xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="5dp"
>
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
/>
</LinearLayout>
代码实现:
String fileName = "/sdcard/y.txt";//文件路径
// 也可以用String fileName = "mnt/sdcard/Y.txt";
String res = "";
try {
FileInputStream fin = new FileInputStream(fileName);
// FileInputStream fin = openFileInput(fileName);
// 用这个就不行了,必须用FileInputStream
int length = fin.available();
byte[] buffer = new byte[length];
fin.read(buffer);
res = EncodingUtils.getString(buffer, "GBK");//依Y.txt的编码类型选择合适的编码,如果不调整会乱码
fin.close();//关闭资源
System.out.println("res--->"+res);
int a=Integer.parseInt(res.substring(3, 5));
int b=Integer.parseInt(res.substring(8, 10));
System.out.println(a+"res--->"+b);//获取的a.b
} catch (Exception e) {
e.printStackTrace();
}
res = EncodingUtils.getString(buffer, "GBK");
这儿charset="GBK"
charset还可以等于"UTF-8"、"BIG-5"
从SD卡上读取图片信息(这儿读取已知路径下的jpeg格式的图片)
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imageview);
ImageView imageView=(ImageView)findViewById(R.id.imageView);
String location="";
if (isSdcard() != null) {
File file = new File(isSdcard());
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].getName().equals("13.jpeg")) {
Bitmap bitmap = BitmapFactory.decodeFile(files[i].getName());
location = isSdcard() + "/" + files[i].getName();
imageView.setImageDrawable(Drawable.createFromPath(location));
System.out.println("13.jpeg的位置:"+location);
}
}
}
}
private String isSdcard() {
File sdcardDir = null;
boolean isSDExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
// 判断SD卡是否存在
if (isSDExist) {
// 如果存在SDcard 就找到跟目录
sdcardDir = Environment.getExternalStorageDirectory();
return sdcardDir.toString();
} else {
return null;
}
}
}
xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="5dp"
>
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
/>
</LinearLayout>
相关文章推荐
- android 代码实现控件之间的间距
- 解決Linux下Android开发真机调试设备不被识别问题
- [Android]在代码里运行另一个程序的方法
- [软件咨询]WPS2012正式版已发布 金山Office移动版4.0发布
- Android笔记-Linux Kernel Ftrace (Function Trace)解析
- android USB如何修改VID具体实现
- Android Mouse实现过程详细笔记
- 深入Android Browser配置管理的详解
- Android Mms之:深入理解对话列表管理
- Android APP与媒体存储服务的交互
- android 多线程技术应用
- Android之采用execSQL与rawQuery方法完成数据的添删改查操作详解
- Android数据类型之间相互转换系统介绍
- 通过Android trace文件分析死锁ANR实例过程
- Android实现左右滑动效果的方法详解
- Android 各国语言缩写及简称详细介绍
- Android 自动判断是电话,网址,EMAIL方法之Linkify的使用
- android IPC之binder通信机制
- Android应用开发中模拟按下HOME键的效果(实现代码)
- Android 读写文件方法汇总