Home | History | Annotate | Download | only in features
      1 page.title=   
      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> </h2>
     10   <ol>
     11     <li><a href="#accessing">     </a></li>
     12     <li><a href="#removable">     </a></li>
     13     <li><a href="#best">  </a></li>
     14   </ol>
     15 </div>
     16 </div>
     17 
     18 <p>  (, )       
     19  , ,   <code>Pictures</code>. 
     20             
     21    . :</p>
     22 
     23 <ul>
     24 <li> {@link android.Manifest.permission#READ_EXTERNAL_STORAGE}
     25  {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE}  
     26        , 
     27        .</li>
     28 <li>  
     29 <a href="{@docRoot}guide/topics/providers/document-provider.html">Storage
     30 Access Framework</a>     
     31   ,   ,     
     32     .</li>
     33 </ul>
     34 
     35 <p>Android N    API  
     36     . </p>
     37 
     38 <h2 id="accessing">     </h2>
     39 
     40 <p>  <code>StorageManager</code>    
     41 <code>StorageVolume</code>.   ,  
     42 <code>StorageVolume.createAccessIntent()</code>  .
     43         .   
     44   ,       , 
     45 <code>StorageManager.getVolumesList()</code>.</p>
     46 
     47 <p>      ,   
     48 <code>Pictures</code>    :</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>       ,  ,
     58          :</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> 1.</strong>  
     63    Pictures.</p>
     64 
     65 <p>   ,    
     66 <code>onActivityResult()</code>   
     67 <code>Activity.RESULT_OK</code>,    ,  URI. 
     68  URI        URI,
     69 
     70 <a href="{@docRoot}guide/topics/providers/document-provider.html">Storage
     71 Access Framework</a>.</p>
     72 
     73 <p>    ,    
     74 <code>onActivityResult()</code>   
     75 <code>Activity.RESULT_CANCELED</code>    .</p>
     76 
     77 <p class="note"><b>.</b>       
     78         .</p>
     79 
     80 <h2 id="removable">     </h2>
     81 
     82 <p>        ,
     83     {@link android.content.BroadcastReceiver},  
     84 {@link android.os.Environment#MEDIA_MOUNTED}, :</p>
     85 
     86 <pre>
     87 &lt;receiver
     88     android:name=".MediaMountedReceiver"
     89     android:enabled="true"
     90     android:exported="true" &gt;
     91     &lt;intent-filter&gt;
     92         &lt;action android:name="android.intent.action.MEDIA_MOUNTED" /&gt;
     93         &lt;data android:scheme="file" /&gt;
     94     &lt;/intent-filter&gt;
     95 &lt;/receiver&gt;
     96 </pre>
     97 
     98 <p>    ,  SD-,   
     99 {@link android.os.Environment#MEDIA_MOUNTED}.  
    100      <code>StorageVolume</code>,    
    101       .    ,
    102      <code>Pictures</code>   :</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">  </h2>
    114 
    115 <p>    URI     ,   
    116        .       
    117 <code>getContentResolver().takePersistableUriPermssion()</code> 
    118 URI   .    URI    
    119     <code>RESULT_OK</code>.  ,     
    120     .</p>
    121 
    122 <p>      ,   
    123    .    ,      
    124  .</p>
    125