編輯:關於Android編程
卸載時,文件會被清除
目錄結構:
a./data/data/package name/cache
b./data/data/package name/file
FileInputStream in = openFileInput(“xixi”);
其中第二個參數是寫出模式,模式有四種:
_1. Context.MODE_PRIVATE:為默認操作模式,代表該文件是私有數據,只能被應用本身訪問,在該模式下,寫入的內容會覆蓋原文件的內容。
_2. Context.MODE_APPEND:此模式會檢查文件是否存在,存在就往文件追加內容,否則就創建新文件。
_3. MODE_WORLD_READABLE:表示當前文件可以被其他應用讀取;
_4. MODE_WORLD_WRITEABLE:表示當前文件可以被其他應用寫入。
假如希望文件被其他應用讀和寫,可以這樣寫:
openFileOutput(“zyc.txt”, Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);
卸載時,文件不會被清除
目錄結構:
mnt/sdcard/
代碼如下:
存數據:
try { //拿取assets數據 open = getResources().getAssets().open(fileName); //限制為本應用使用的資源 //寫到/data/data//files fileOutputStream = openFileOutput(fileName, Context.MODE_PRIVATE); int len; //每次操作1M byte[] buffer = new byte[1024]; //循環讀寫數據,直到文件處理完 while ((len = open.read(buffer)) != -1) { //每次讀1M數據 open.read(buffer); //文件長度不確定,及時將buffer中的數據寫出去 fileOutputStream.write(buffer, 0, len); } //防止數據丟失,刷新 fileOutputStream.flush(); Toast.makeText(MainActivity.this, "文件" + fileName + "存取成功!", Toast.LENGTH_SHORT).show(); } catch (IOException e) { Log.e(TAG, "讀取assets文件IO異常" + e); } finally { //防止leak,關閉流 if (null != open) { try { open.close(); } catch (IOException e) { Log.e(TAG, "關閉輸入流失敗" + e); } } if (null != fileOutputStream) { try { fileOutputStream.close(); } catch (IOException e) { Log.e(TAG, "關閉輸出流失敗" + e); } } }
讀數據:
try { FileInputStream fis = openFileInput(fileName); int available = fis.available(); byte[] buffer = new byte[available]; fis.read(buffer); Toast.makeText(MainActivity.this, new String(buffer).trim().toString(), Toast.LENGTH_SHORT).show(); System.out.println(new String(buffer).trim().toString()); } catch (FileNotFoundException e) { Log.e(TAG, "讀取文件失敗" + e); } catch (IOException e) { Log.e(TAG, "讀取文件IO異常" + e); }
注釋很清晰,就不做說明了。
存儲圖片到私有目錄:
try { //拿取assets數據 open = getResources().getAssets().open(fileName); //限制為本應用使用的資源 //寫到/data/data//files fileOutputStream = openFileOutput(fileName, Context.MODE_PRIVATE); Bitmap.CompressFormat localCompressFormat = Bitmap.CompressFormat.JPEG; Bitmap bitmap = BitmapFactory.decodeStream(open); bitmap.compress(localCompressFormat, 100, fileOutputStream); Toast.makeText(MainActivity.this, "文件" + fileName + "存取成功!", Toast.LENGTH_SHORT).show(); } catch (IOException e) { Log.e(TAG, "讀取assets文件IO異常" + e); } finally { //防止leak,關閉流 if (null != open) { try { open.close(); } catch (IOException e) { Log.e(TAG, "關閉輸入流失敗" + e); } } if (null != fileOutputStream) { try { fileOutputStream.close(); } catch (IOException e) { Log.e(TAG, "關閉輸出流失敗" + e); } } }
顯示存儲在私有目錄的圖片:
讀圖片:
//顯示圖片 try { fis = openFileInput(fileName); int len = fis.available(); //將流轉為byte byte[] buffer = new byte[len]; fis.read(buffer); // Toast.makeText(MainActivity.this,new String(buffer).trim(),Toast.LENGTH_SHORT).show(); showPic(buffer); } catch (FileNotFoundException e) { Log.e(TAG, "找不到" + fileName + e); } catch (IOException e) { Log.e(TAG, fileName + "IO異常" + e); }finally { if (null != fis){ try { fis.close(); } catch (IOException e) { Log.e(TAG,"fis關閉IO異常" + e); } } }
顯示圖片showPic():
{ Log.i(TAG,"圖片byte" + new String(b)); Dialog dialog = new Dialog(MainActivity.this, R.style.my_dialog); View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.show_pic, null); dialog.setContentView(view); ImageView iv = (ImageView) view.findViewById(R.id.iv_show); Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length); if (null != bitmap) { Toast.makeText(MainActivity.this, "圖片成功了", Toast.LENGTH_LONG).show(); iv.setImageBitmap(bitmap); } else { Toast.makeText(MainActivity.this, "圖片為空", Toast.LENGTH_LONG).show(); } Window window = dialog.getWindow(); WindowManager.LayoutParams attributes = window.getAttributes(); attributes.height = (int) (A.getDisplayMetrics(MainActivity.this).heightPixels * 0.8); attributes.width = (int) (A.getDisplayMetrics(MainActivity.this).widthPixels * 0.66); window.setAttributes(attributes); dialog.show(); }
存儲文本文件到SD卡:
try { //拿取assets數據 open = getResources().getAssets().open(fileName); //首先要判斷sd卡,是否已經掛載 boolean b = ExistSDCard(); if (!b) { Toast.makeText(this, "沒有sd卡", Toast.LENGTH_SHORT).show(); return; } //目錄是否存在 File dir = new File(A.textPath); if (!dir.exists()) { dir.mkdir(); } //文件是否存在 path = A.textPath + "testFile.txt"; File file = new File(path); if (!file.exists()) { try { file.createNewFile(); } catch (Exception e) { e.printStackTrace(); } } fileOutputStream = new FileOutputStream(path); int len; //每次操作1M byte[] buffer = new byte[1024]; //循環讀寫數據,直到文件處理完 while ((len = open.read(buffer)) != -1) { //每次讀1M數據 open.read(buffer); //文件長度不確定,及時將buffer中的數據寫出去 fileOutputStream.write(buffer, 0, len); } //防止數據丟失,刷新 fileOutputStream.flush(); Toast.makeText(MainActivity.this, "文件" + fileName + "存取成功!", Toast.LENGTH_SHORT).show(); } catch (IOException e) { Log.e(TAG, "讀取assets文件IO異常" + e); } finally { //防止leak,關閉流 if (null != open) { try { open.close(); } catch (IOException e) { Log.e(TAG, "關閉輸入流失敗" + e); } } if (null != fileOutputStream) { try { fileOutputStream.close(); } catch (IOException e) { Log.e(TAG, "關閉輸出流失敗" + e); } } } }
existSDCard:
private boolean existSDCard() { if (android.os.Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED)) { return true; } else return false; }
讀sd卡文本文件:
try { FileInputStream fis = new FileInputStream(path); int ll = fis.available(); byte[] buffer = new byte[ll]; fis.read(buffer, 0, ll); System.out.println(new String(buffer).toString()); Toast.makeText(this, new String(buffer).toString(), Toast.LENGTH_SHORT).show(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
存儲圖片到sd:
try { //拿取assets數據 open = getResources().getAssets().open(fileName); //判斷sd卡 if(!existSDCard()){ Toast.makeText(this,"沒有SD卡",Toast.LENGTH_SHORT).show(); return; } File dir = new File(A.picPath); if(!dir.exists()){ dir.mkdir(); } path = A.picPath + "picTest.jpg"; File file = new File(path); if(!file.exists()){ file.createNewFile(); } fileOutputStream = new FileOutputStream(path); Bitmap.CompressFormat localCompressFormat = Bitmap.CompressFormat.JPEG; Bitmap bitmap = BitmapFactory.decodeStream(open); bitmap.compress(localCompressFormat, 100, fileOutputStream); //防止數據丟失,刷新 fileOutputStream.flush(); Toast.makeText(MainActivity.this, "文件" + fileName + "存取成功!", Toast.LENGTH_SHORT).show(); } catch (IOException e) { Log.e(TAG, "讀取assets文件IO異常" + e); } finally { //防止leak,關閉流 if (null != open) { try { open.close(); } catch (IOException e) { Log.e(TAG, "關閉輸入流失敗" + e); } } if (null != fileOutputStream) { try { fileOutputStream.close(); } catch (IOException e) { Log.e(TAG, "關閉輸出流失敗" + e); } } }
從SD讀取圖片並顯示:
try { FileInputStream fis = new FileInputStream(path); int len = fis.available(); byte[] buffer = new byte[len]; fis.read(buffer,0,len); showPic(buffer); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
NumberPickerViewanother NumberPicker with more flexible attributes on Android platfor
Okio庫是一個由square公司開發的,它補充了java.io和java.nio的不足,以便能夠更加方便,快速的訪問、存儲和處理你的數據。而OkHttp的底層也使用該庫
Android基礎入門教程——8.4.3 Android動畫合集之屬性動畫-初見標簽(空格分隔): Android基礎入門教程本節引言: 本節給帶
首先看看本節的流程: 計算圓的頂點坐標: 我們先要明白OpenglES中圓是怎麼畫的,前面我們已經知道三角形扇的繪制方式,我們的圓其實也可