存儲(chǔ)在內(nèi)部還是外部祟身?
*Internal storage(內(nèi)部)
** getFileDir():返回一個(gè)File,代表了我們app的internal目錄奥务。
** getCacheDir():返回一個(gè)File,代表了我們app的internal緩存目錄。
*External storage(外部)
MainActivity中創(chuàng)建文檔寫入數(shù)據(jù):
testFileDemo();
/**
*the demo for test file.
*/
private void testFileDemo(){
//create a new file of test.txt in the internal storage
File file = new File(getFilesDir(),"test.txt");
//打印路徑
Log.i("MainActivity","getFileDir:"+getFileDir().getAbsolutePath());
Log.i("MainActivity","file path:"+file.getAbsolutePath());
String string = "That is right!";
//拋出異常
try{
boolean isSuccess = file.createNewFile();
} catch (IOException e){
Log.i("MainActivity","test.txt create error:"+e.toString());
e.printStackTrace();
}
try{
FileOutputStream fileOutputStraem = openFileOutput("test2.txt",Context.MODE_PRIVATE);
try{
fileOutputStream.write(string.getBytes());
fileOutputStream.close();
} catch(FileNotFoundException e){
e.printStackTrace();
} catch (FileNotFoundException e){
e.printStackTrace();
}
//check externalcstorage state
String state = Environment.getExternalStorageState();
if(TextUtils.equals(state,Environment.MEDIA_MOUNTED)){
......
}
}
讀取各目錄下的文件
- 操作assets目錄下的文件
void testAssets(){
//第一種袜硫,直接讀取路徑
WebView webView = new Webview(this);
webView.loadUrl("file:///android_asset/test.html");
try{
//open的只能是文件氯葬,不能使文件夾
InputStream inputStream = getResources().getAssets().open("test.html");
} catch (IOException e){
e.printStackTrace();
Toast.makeText(MainActivity.this,"文件讀取異常",Toast.LENGTH_SHORT).show();
}
//讀列表
string[] filenames = getAssets().list("images");
//讀圖片
InputStream inputStream = getAssets().open("images/dream.jpg");
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
ImageView imageView = new ImageView(this);
imageView.setImageBitmap(bitmap);
//讀音樂
AssetsFileDescriptor assetFileDescriptor = getAssets().openFd("libai.mp3");
MediaPlayer player = new MediaPlayer();
player.reset();
player.setDataSource(assetFileDescriptor.getFileDescriptor(),
assetFileDescriptor.getStartOffset(),
assetFileDescriptor.getLength());
player.prepare();
player.start();
}
- 操作res目錄下的文件
- 操作raw目錄下的文件
void testResFile(){
InputStream inputStream = getResources().openRawRescource(R.raw.libai);
getRescource().getColor(R.color.abc_background_cache_hint_selector_material_dark);
getRescource().getDrawable();
}
- 操作sd卡文件
void testSDCard(){
File file = new File("/sdcard/test/a.txt");
String filePath = Environment.getExternalStorageDirectory().getAbsolutePath();
Environment.getDataDirectory();//獲取Android中的data數(shù)據(jù)目錄
Environment.getDownloadCacheDirectory();
Environment.getExternalStorageDirectory();
}
獲取External存儲(chǔ)的權(quán)限:android.permission.EXTERNAL_STORAGE
在AndroidMainifest.xml文件中注冊(cè)權(quán)限:
- 獲取申請(qǐng)上網(wǎng)權(quán)限:
<uses-permission android:name="android.permission.INTERENT"/>
- 寫外部存儲(chǔ)的權(quán)限(*注:應(yīng)用本身就可以讀寫內(nèi)部?jī)?nèi)存婉陷,所以無需注冊(cè)):
<uses-permission android:name = "android.perission.WRITE_EXTERNAL_STORAGE"/>
應(yīng)用選擇安裝在內(nèi)部空間或外部空間(外部sd卡):
android:installLocation = "preferExternal"