1. 程式人生 > >Android data目錄讀寫檔案

Android data目錄讀寫檔案

 //把圖片檔案複製到指定目錄   //PicID是drawable的圖片資源ID public void copyImage2Data(Integer PicID) { Log.d(TAG, "mythou copyImage2Data----->Enter PicID="+PicID); try { //計算圖片存放全路徑 String LogoFilePath = LogoFileRoot + LogoFileName; File dir
= new File(LogoFileRoot); //如果資料夾不存在,建立一個(只能在應用包下面的目錄,其他目錄需要申請許可權 OWL) if(!dir.exists()) { Log.d(TAG, "mythou copyImage2Data----->dir not exist"); } boolean result = dir.mkdirs(); Log.d(TAG, "dir.mkdirs()----->result =
"+result); // 獲得封裝 檔案的InputStream物件 InputStream is = mContext.getResources().openRawResource(PicID); Log.d(TAG, "copyImage2Data----->InputStream open"); FileOutputStream fos = new FileOutputStream(LogoFilePath);
byte[] buffer = new byte[8192]; System.out.println("3"); int count = 0; // 開始複製Logo圖片檔案 while((count=is.read(buffer)) > 0) { fos.write(buffer, 0, count); System.out.println("4"); } fos.close(); is.close(); } catch(Exception e) { e.printStackTrace(); } }