您的位置:首页 > 移动开发 > Android开发

Android文件读取

2016-04-05 22:47 381 查看
读取 Asserts文件夹中的文件

读取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 数据

支持的数据类型

int

boolear

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”

PreferenceActivity

PreferenceScreen

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息