1 page.title=Truy cp Th mc theo Phm vi 2 page.keywords=preview,sdk,scoped directory access 3 page.tags=androidn 4 5 @jd:body 6 7 <div id="qv-wrapper"> 8 <div id="qv"> 9 <h2>Trong ti liu ny</h2> 10 <ol> 11 <li><a href="#accessing">Truy cp mt Th mc lu tr bn ngoi</a></li> 12 <li><a href="#removable">Truy cp mt Th mc trn Phng tin tho lp c</a></li> 13 <li><a href="#best">Thc hnh Tt nht</a></li> 14 </ol> 15 </div> 16 </div> 17 18 <p>Cc ng dng nh ng dng nh thng ch cn truy cp n cc th mc quy nh trong 19 b nh ngoi nh th mc <code>Pictures</code>. Cc phng php 20 hin ti truy cp b nh lu tr ngoi vn cha c thit k d dng cho php 21 truy cp th mc ch cho nhng kiu ng dng ny. V d:</p> 22 23 <ul> 24 <li>Vic yu cu {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} 25 hoc {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} trong bn k khai ca bn 26 s cho php truy cp n tt c cc th mc cng khai trn b nh lu tr ngoi, m c th 27 cp nhiu quyn truy cp hn nhng g ng dng ca bn cn.</li> 28 <li>S dng 29 <a href="{@docRoot}guide/topics/providers/document-provider.html">Khun kh 30 Truy cp Kho lu tr</a> thng khin ngi dng chn th mc 31 thng qua UI h thng. y l iu khng cn thit nu ng dng ca bn lun truy cp cng 32 mt th mc bn ngoi.</li> 33 </ul> 34 35 <p>Android N cung cp mt API mi c n gin ha truy cp 36 cc th mc lu tr bn ngoi thng dng. </p> 37 38 <h2 id="accessing">Truy cp mt Th mc lu tr bn ngoi</h2> 39 40 <p>S dng lp <code>StorageManager</code> ly thc th 41 <code>StorageVolume</code> ph hp. Sau to mt nh bng cch gi phng thc 42 <code>StorageVolume.createAccessIntent()</code> ca thc th . 43 S dng nh ny truy cp cc th mc lu tr bn ngoi. ly danh sch 44 tt c cc a, bao gm cc a media c th tho lp, hy s dng 45 <code>StorageManager.getVolumesList()</code>.</p> 46 47 <p>on m sau l mt v d v cch m th mc 48 <code>Pictures</code> trong b nh lu tr chnh c chia s:</p> 49 50 <pre> 51 StorageManager sm = (StorageManager)getSystemService(Context.STORAGE_SERVICE); 52 StorageVolume volume = sm.getPrimaryVolume(); 53 Intent intent = volume.createAccessIntent(Environment.DIRECTORY_PICTURES); 54 startActivityForResult(intent, request_code); 55 </pre> 56 57 <p>H thng s c gng cp quyn truy cp ti th mc bn ngoi v nu 58 cn s xc nhn quyn truy cp vi ngi dng bng mt UI c n gin ha:</p> 59 60 <img src="{@docRoot}preview/images/scoped-folder-access-framed.png" srcset="{@docRoot}preview/images/scoped-folder-access-framed.png 1x, 61 {@docRoot}preview/images/scoped-folder-access-framed_2x.png 2x" /> 62 <p class="img-caption"><strong>Hnh 1.</strong> Mt ng dng yu cu 63 truy cp ti th mc Pictures.</p> 64 65 <p>Nu ngi dng cp quyn truy cp, h thng s gi phng thc ghi 66 <code>onActivityResult()</code> ca bn vi m kt qu l 67 <code>Activity.RESULT_OK</code> v d liu nh c cha URI. Hy s dng 68 URI c cung cp truy cp thng tin th mc, ging nh s dng cc URI 69 c tr v bi 70 <a href="{@docRoot}guide/topics/providers/document-provider.html">Khun kh 71 Truy cp Kho lu tr</a>.</p> 72 73 <p>Nu ngi dng khng cp quyn truy cp, h thng s gi phng thc ghi 74 <code>onActivityResult()</code> ca bn vi m kt qu l 75 <code>Activity.RESULT_CANCELED</code> v d liu nh c gi tr null.</p> 76 77 <p class="note"><b>Lu </b>: Ly quyn truy cp ti mt th mc bn ngoi c ch nh 78 cng s cp quyn truy cp ti cc th mc con thuc th mc .</p> 79 80 <h2 id="removable">Truy cp mt Th mc trn phng tin tho lp c</h2> 81 82 <p> s dng Truy cp Th mc theo Phm vi nhm truy cp cc th mc trn phng tin c th tho lp, 83 trc ht, hy thm mt {@link android.content.BroadcastReceiver} lng nghe 84 thng bo {@link android.os.Environment#MEDIA_MOUNTED}, v d:</p> 85 86 <pre> 87 <receiver 88 android:name=".MediaMountedReceiver" 89 android:enabled="true" 90 android:exported="true" > 91 <intent-filter> 92 <action android:name="android.intent.action.MEDIA_MOUNTED" /> 93 <data android:scheme="file" /> 94 </intent-filter> 95 </receiver> 96 </pre> 97 98 <p>Khi ngi dng kt ni mt phng tin c th tho lp nh th SD th h thng s gi mt thng bo 99 {@link android.os.Environment#MEDIA_MOUNTED}. Thng bo ny 100 s cung cp mt i tng <code>StorageVolume</code> trong d liu nh m bn c th 101 s dng truy cp cc th mc trn phng tin c th tho lp . V d sau 102 s truy cp th mc <code>Pictures</code> trn phng tin c th tho lp:</p> 103 104 <pre> 105 // BroadcastReceiver has already cached the MEDIA_MOUNTED 106 // notification Intent in mediaMountedIntent 107 StorageVolume volume = (StorageVolume) 108 mediaMountedIntent.getParcelableExtra(StorageVolume.EXTRA_STORAGE_VOLUME); 109 volume.createAccessIntent(Environment.DIRECTORY_PICTURES); 110 startActivityForResult(intent, request_code); 111 </pre> 112 113 <h2 id="best">Thc hnh Tt nht</h2> 114 115 <p>Khi c th, hy duy tr URI truy cp th mc bn ngoi bn khng phi 116 lp li yu cu ngi dng cp quyn truy cp. Khi ngi dng cp quyn truy cp, hy gi 117 <code>getContentResolver().takePersistableUriPermssion()</code> vi 118 URI truy cp th mc. H thng s duy tr URI v cc yu cu 119 truy cp sau ny s tr v <code>RESULT_OK</code> v khng hin th UI xc nhn cho 120 ngi dng na.</p> 121 122 <p>Nu ngi dng t chi quyn truy cp n mt th mc bn ngoi th ng 123 yu cu truy cp li ngay lp tc. Lp i lp li yu cu truy cp s dn n tri nghim 124 ngi dng khng tt.</p> 125