Android文件读取
2016-04-05 22:47
381 查看
读取 Asserts文件夹中的文件
读取Raw文件夹下的文件
安卓内部存储的文件数据
安卓外部存储的文件数据读取
SharedPreferences 数据
支持的数据类型
SharedPreferences 读写操作
读取
PreferencesActiviry的sdcard 存储的
专业的Setting
数据库
获取未安装的APK信息
PS:不是在Activity下
取出大图片
PS:不是在Activity下
PS:不是在Activity下
boolear
float
long
String
PreferenceScreen
读取Raw文件夹下的文件
安卓内部存储的文件数据
安卓外部存储的文件数据读取
SharedPreferences 数据
支持的数据类型
SharedPreferences 读写操作
读取
PreferencesActiviry的sdcard 存储的
专业的Setting
数据库
获取未安装的APK信息
1.读取 Asserts文件夹中的文件
InputStream is = getResources().getAsserts().open("Asserts文件夹下的文件名"); InputStreamReader isr=new InputStreamReader(is , "UTF-8" ); BufferedReader br=new BufferedReader(isr); String str=""; StrigBuffer sb==new StringBuffer(); while((str=br.readLine())!=null) { sb.append(str); }
PS:不是在Activity下
context.getResources()
2.读取Raw文件夹下的文件
在 res 文件夹下创建一个名为 Raw 的文件夹, 我一般把大的图片放在raw文件下.InputStream is = getResources().openRawResource( R.raw.info ); //info 为文件名 InputStreamReader isr=new InputStreamReader(is, "UTF-8"); BufferedReader br=new BufferedReader(isr); String str=""; StrigBuffer sb==new StringBuffer(); while((str=br.readLine())!=null) { sb.append(str); }
取出大图片
public static Bitmap getBitmap(int rawResource){ BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inPreferredConfig = Bitmap.Config.RGB_565; opt.inPurgeable = true; opt.inInputShareable = true; InputStream is = Global.globalContext.getResources().openRawResource(rawResource); Bitmap bitmap = BitmapFactory.decodeStream(is,null, opt); try { is.close(); } catch (IOException e) { e.printStackTrace(); } return bitmap; }
PS:不是在Activity下
context.getResources()
3.安卓内部存储的文件数据
String fileName="test"; /* 一、 写入 */ FileOutputStream fos = openFileOutput( filename, Content.MODE_PRIVATE); //写入模式为私有的 OutputStreamWriter osw=new OutputStreamWriter(fos, "UTF-8" ); osw.writeString("fadsfdasfasfsdafasfdasfasdfasdf名字啊!"); osw.flush(); fos.flush(); osw.close() fos.close(); /* 二、读取 */ FileInputStream fis = openFileInput( fileName ); InputStreamReader isr = new InputStreamReader( fis , "UTF-8" ); char[] input=new char[ fis.available() ]; isr.read(input); isr.close(); fis.close(); System.out.println( input.toString() );
PS:不是在Activity下
context.openFileOutput()
4.安卓外部存储的文件数据–读取
File sdcard = Environment.getExternalStorageDirectory(); //获取SDCard的路径 File myFile=new File ( scdrd, "TextFolder" ); //后面,为名字 if( ! sdcard.exists( ) ) { //不存在SDCard } myFile.createNewFile(); // 写入 FileOutputStream fos = new FileOutputStream(myFile); OutputStreamWriter osw = new OutputStreamWitter(fos, "UTF-8"); osw.witer("afadsfdasfa"); osw.flush(); osw.close(); fos.close();
5.SharedPreferences 数据
支持的数据类型
intboolear
float
long
String
SharedPreferences 读写操作
SharedPrefences prefences =getPreferences(Activity.MODE_PRIVATE); Editor editor = prefences.edit(); String key="myValue"; editor.putString( myValue, "123456"); editor.putInt("ab", 1) editor.putFloat("flos" ,2.4 ); boolear ok = editor.commit(); if(ok) { 输出成功 } else { 输出失败 }
读取
SharedPrefences prefences =getPreferences(Activity.MODE_PRIVATE); String str = preferences.getString(key, "数值不存在时的默认值" ); int i=preferences.getInt("ab", 0 ); float f = preferences.getFloat("flos" , 0.0f );
PreferencesActiviry的sdcard 存储的
File sdcard = Environment.getExternalStroageDirectory () ; File myFile = new File (sdcard ,"fileName.txt") ; //文件名 //注意权限.....android.permission.WRITE_EXTEAGE_STORAGE.... if( !sdcard.exists()) { //没有内存卡 return ; } FIleInputStream fis = new FileInputSteam(myFile); InputStreamReader isr = new InputStreamReader(fis ,"UTF-8" );
专业的”Setting”
PreferenceActivityPreferenceScreen
6.数据库
class db extends SQLiteOpenHelper { //构造方法 public db(Context context,String name,CursorFactoty factory ,int version) {} @override oncreate(SQLiteDatabase dbs) { db.execSQL("create table "+"sqlname"+"(name text,path text,)") } @override public void onUpgrade() { } }
7.获取未安装的APK信息
public Drawable getApkIcon(Context context, String apkPath) { PackageManager pm = context.getPackageManager(); PackageInfo info=pm.getPackageArchiveInfo(apkPath,PackageManager.GET_ACTIVITIES); if(info != null) { ApplicationInfo appInfo = info.applicationInfo; appInfo.sourceDir = apkPath; appInfo.publicSourceDir = apkPath; try { String label = appInfo.loadLabel(pm).toString(); Log.e("应用中文名:", label); String packageName = appInfo.packageName; Log.e("包名:", packageName); return appInfo.loadIcon(pm); } catch(OutOfMemoryError e) { Log.e("ApkIconLoader", e.toString()); } } return null; }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件