{"id":4256,"date":"2018-07-16T22:33:34","date_gmt":"2018-07-16T14:33:34","guid":{"rendered":"http:\/\/www.bookcard.net\/wordpress\/?p=4256"},"modified":"2018-07-21T11:18:11","modified_gmt":"2018-07-21T03:18:11","slug":"android_game_file_read_write","status":"publish","type":"post","link":"https:\/\/www.bookcard.net\/wordpress\/android_game_file_read_write\/","title":{"rendered":"Android\u904a\u6232\u4e4b\u6587\u6a94\u8b80\u5beb"},"content":{"rendered":"<figure id=\"attachment_4257\" aria-describedby=\"caption-attachment-4257\" style=\"width: 1024px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/07\/Android_Game_File_Read_Write.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4257\" src=\"https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/07\/Android_Game_File_Read_Write.jpg\" alt=\"\" width=\"1024\" height=\"768\" srcset=\"https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/07\/Android_Game_File_Read_Write.jpg 1024w, https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/07\/Android_Game_File_Read_Write-300x225.jpg 300w, https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/07\/Android_Game_File_Read_Write-768x576.jpg 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption id=\"caption-attachment-4257\" class=\"wp-caption-text\">Android\u904a\u6232\u4e4b\u6587\u6a94\u8b80\u5beb<\/figcaption><\/figure>\n<p>\u5728Android\u904a\u6232\u958b\u767c\u6642\u9700\u8981\u8b80\u5165\u5927\u91cfAsset(\u8cc7\u6e90).\u5982\u300e3D\u6a21\u578b\u300f\u300e\u7d0b\u7406\u300f\u300e\u97f3\u983b\u300f\u300e\u5730\u578b\u300f\u7b49Asset(\u8cc7\u6e90)\u6587\u6a94.Android\u5efa\u8b70\u628a\u8cc7\u6e90\u5b58\u653e\u65bc\u300eres\u300f\u76ee\u9304,\u4f46\u5b83\u4e0d\u9069\u5408\u5b58\u653e\u539f\u751f\u8cc7\u6e90. \u5728\u904a\u6232\u958b\u767c\u6642\u4f7f\u7528\u300eassets\u300f\u76ee\u9304,\u6240\u6709\u904a\u6232\u8cc7\u6e90\u6587\u6a94\u5b58\u653e\u8a72\u76ee\u9304\u4e0b.\u800c\u4e14\u9084\u53ef\u4ee5\u6307\u5b9a\u76ee\u9304\u7d50\u69cb. \u8981\u8a2a\u554f\u300eassets\u300f\u76ee\u9304\u9700AssetManager\u8cc7\u6e90\u7ba1\u7406\u5668:<\/p>\n<p>\u300eassets\u300f\u76ee\u9304\u4f4d\u65bc\u300e\\app\\src\\main\\assets\u300f\u9019\u9ede\u8207Eclipse\u6709\u6240\u4e0d\u540c.<\/p>\n<p>AssetManager asset_manager = context.getAssets();<\/p>\n<p>\u6253\u958bAsset\u6587\u6a94\u4e26\u8fd4\u56de\u8f38\u5165\u6d41InputStream<\/p>\n<p>InputStream input_stream = asset_manager.open(file_name);<\/p>\n<p>\u8b80\u53d6\u300e\u5916\u90e8\u7de9\u5b58\u300fSD\u5361<\/p>\n<p>Asset\u53ea\u9069\u5408\u7528\u65bc\u8b80\u53d6\u904a\u6232\u8cc7\u6e90.\u82e5\u5728\u904a\u6232\u904b\u884c\u6642\u8b80\u5beb\u6587\u6a94\u6578\u64da.\u82e5\u8a2a\u554fSD\u5361\u9700\u8981\u52a0\u5165\u5ea6\u5beb\u6b0a\u9650:<\/p>\n<p>&lt;uses-permission android:name=&#8221;android.permission.WRITE_EXTERNAL_STORAGE&#8221;\/&gt;<\/p>\n<p>&lt;uses-permission android:name=&#8221;android.permission.READ_EXTERNAL_STORAGE&#8221;\/&gt;<\/p>\n<p>\u7136\u5f8c\u9700\u8981\u78ba\u5b9a\u624b\u6a5f\u662f\u5426\u88dd\u6709SD\u5361<\/p>\n<p>\u7372\u53d6\u300e\u5916\u90e8\u7de9\u5b58\u300fSD\u5361\u88dd\u614b.\u5176\u5be6\u73fe\u5728\u624b\u6a5f\u8f09\u56fa\u614b\u786c\u76e4\u5283\u51fa\u90e8\u5206\u7a7a\u9593\u4f5c\u70ba\u300e\u5916\u90e8\u7de9\u5b58\u300f<\/p>\n<p>String state = Environment.getExternalStorageState();<\/p>\n<p>Environment.MEDIA_MOUNTED:SD\u5361\u63d2\u5165\u4e26\u53ef\u6b63\u5e38\u8b80\u5beb<\/p>\n<p>Environment.MEDIA_MOUNTED_READ_ONLY:SD\u5361\u5df2\u63d2\u5165,\u4f46\u53ea\u80fd\u8b80\u53d6<\/p>\n<p>\u7372\u53d6\u300e\u5916\u90e8\u7de9\u5b58\u300f\u8def\u5f91<\/p>\n<p>File file_path = Environment.getExternalStorageDirectory();<\/p>\n<p>\u4e26\u4e0d\u5efa\u8b70\u5728\u300e\u5916\u90e8\u7de9\u5b58\u300fSD\u5361\u8b80\u5beb.\u4e00SD\u5361\u53ef\u80fd\u88ab\u5f48\u51fa.\u4e8c\u53ef\u80fd\u9700\u8981\u7528\u6236\u6388\u6b0a.\u5982\u679c\u6578\u64da\u8f03\u5c0f\u5efa\u8b70\u5beb\u5165\u300e\u5167\u90e8\u5b58\u5132\u300f<\/p>\n<p>APP\u5167\u90e8\u5b58\u5132\u7de9\u5b58\u76ee\u9304\u300e\/data\/data\/&lt;\u00a0package\u00a0name\u00a0&gt;\/files\/\u300f<\/p>\n<p>File file_path = context.getFilesDir() ;<\/p>\n<p>\u751f\u6210\u76ee\u9304<\/p>\n<p>file_path.mkdir();<\/p>\n<p>\u9023\u63a5\u8def\u5f91<\/p>\n<p>File file\u00a0 = new File(file_path,file_name);<\/p>\n<p>\u6253\u958b\u6587\u6a94\u4e26\u8fd4\u56de\u8f38\u5165\u6d41<\/p>\n<p>InputStream input_stream = new FileInputStream(file);<\/p>\n<p>\u6253\u958b\u6587\u6a94\u4e26\u8fd4\u56de\u8f38\u51fa\u6d41<\/p>\n<p>OutputStream output_stream = new FileOutputStream(file);<\/p>\n<table width=\"548\">\n<tbody>\n<tr>\n<td width=\"274\">\u7372\u53d6\u6307\u5b9a\u5beb\u5165\/\u8b80\u53d6\u8def\u5f91<\/td>\n<td width=\"274\">\u5b58\u5132\u4f4d\u7f6e<\/td>\n<\/tr>\n<tr>\n<td width=\"274\">Environment.getExternalStorageDirectory()<\/td>\n<td width=\"274\">\/mnt\/sdcard\/<\/td>\n<\/tr>\n<tr>\n<td width=\"274\">context.getExternalFilesDir()<\/td>\n<td width=\"274\">\/mnt\/sdcard\/Android\/data\/&lt;\u00a0package\u00a0name\u00a0&gt;\/files\/<\/td>\n<\/tr>\n<tr>\n<td width=\"274\">context.getExternalCacheDir()<\/td>\n<td width=\"274\">\/mnt\/sdcard\/Android\/data\/&lt;\u00a0package\u00a0name\u00a0&gt;\/cach\/<\/td>\n<\/tr>\n<tr>\n<td width=\"274\">context.getFilesDir()<\/td>\n<td width=\"274\">\/data\/data\/&lt;\u00a0package\u00a0name\u00a0&gt;\/files\/<\/td>\n<\/tr>\n<tr>\n<td width=\"274\">context.getCacheDir()<\/td>\n<td width=\"274\">\/data\/data\/&lt;\u00a0package\u00a0name\u00a0&gt;\/cach\/<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<table width=\"548\">\n<tbody>\n<tr>\n<td width=\"236\">\u7372\u53d6\u6307\u5b9a\u5beb\u5165\/\u8b80\u53d6\u8def\u5f91<\/td>\n<td width=\"312\">\u7c21\u4ecb<\/td>\n<\/tr>\n<tr>\n<td width=\"236\">Environment.getExternalStorageDirectory()<\/td>\n<td width=\"312\">\u5916\u90e8\u5b58\u5132<\/td>\n<\/tr>\n<tr>\n<td width=\"236\">context.getExternalFilesDir()<\/td>\n<td width=\"312\">\u5916\u90e8\u5b58\u5132<\/td>\n<\/tr>\n<tr>\n<td width=\"236\">context.getExternalCacheDir()<\/td>\n<td width=\"312\">\u5916\u90e8\u81e8\u6642\u5b58\u5132<\/td>\n<\/tr>\n<tr>\n<td width=\"236\">context.getFilesDir()<\/td>\n<td width=\"312\">\u5167\u90e8\u5b58\u5132<\/td>\n<\/tr>\n<tr>\n<td width=\"236\">context.getCacheDir()<\/td>\n<td width=\"312\">\u5167\u90e8\u81e8\u6642\u7de9\u5b58<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Android\u904a\u6232\u958b\u767c\u6642\u9700\u8981\u8b80\u5165\u5927\u91cfAsset(\u8cc7\u6e90).\u5982\u300e3D\u6a21\u578b\u300f\u300e\u7d0b\u7406\u300f\u300e\u97f3\u983b\u300f\u300e\u5730\u578b\u300f\u7b49Asset( &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.bookcard.net\/wordpress\/android_game_file_read_write\/\" class=\"more-link\">\u7e7c\u7e8c\u95b1\u8b80 <span class=\"screen-reader-text\">Android\u904a\u6232\u4e4b\u6587\u6a94\u8b80\u5beb<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[2],"class_list":["post-4256","post","type-post","status-publish","format-standard","hentry","category-android","tag-android","entry"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/posts\/4256","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/comments?post=4256"}],"version-history":[{"count":6,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/posts\/4256\/revisions"}],"predecessor-version":[{"id":4291,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/posts\/4256\/revisions\/4291"}],"wp:attachment":[{"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/media?parent=4256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/categories?post=4256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/tags?post=4256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}