<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                > 編寫:[jdneo](https://github.com/jdneo) - 原文:[http://developer.android.com/training/beam-files/receive-files.html](http://developer.android.com/training/beam-files/receive-files.html) Android Beam文件傳輸將文件拷貝至接收設備上的一個特殊目錄。同時使用Android Media Scanner掃描拷貝的文件,并在[MediaStore](http://developer.android.com/reference/android/provider/MediaStore.html) provider中為媒體文件添加對應的條目記錄。這節課將向你展示當文件拷貝完成時要如何響應,以及在接收設備上應該如何定位拷貝的文件。 ### 響應請求并顯示數據 當Android Beam文件傳輸將文件拷貝至接收設備后,它會發布一個通知,該通知包含有一個[Intent](http://developer.android.com/reference/android/content/Intent.html),該Intent擁有:[ACTION_VIEW](http://developer.android.com/reference/android/content/Intent.html#ACTION_VIEW)這一Action,首個被傳輸文件的MIME類型,以及一個指向第一個文件的URI。當用戶點擊了這個通知后,Intent會被發送至系統。為了讓你的應用程序能夠響應這個Intent,我們需要為響應的[Activity](# "An activity represents a single screen with a user interface.")所對應的[<](http://developer.android.com/guide/topics/manifest/activity-element.html)[activity](# "An activity represents a single screen with a user interface.")>標簽添加一個[`<intent-filter>`](http://developer.android.com/guide/topics/manifest/intent-filter-element.html)標簽,在[`<intent-filter>`](http://developer.android.com/guide/topics/manifest/intent-filter-element.html)標簽中,添加下面的子標簽: [`<action android:name="android.intent.action.VIEW" />`](http://developer.android.com/guide/topics/manifest/action-element.html) 該標簽用來匹配從通知發出的Intent,這些Intent具有[ACTION_VIEW](http://developer.android.com/reference/android/content/Intent.html#ACTION_VIEW)這一Action。 [`<category android:name="android.intent.category.CATEGORY_DEFAULT" />`](http://developer.android.com/guide/topics/manifest/category-element.html) 該標簽用來匹配不含有顯式Category的[Intent](http://developer.android.com/reference/android/content/Intent.html)對象。 [`<data android:mimeType="mime-type" />`](http://developer.android.com/guide/topics/manifest/data-element.html) 匹配一個MIME類型。僅僅指定那些你的應用能夠處理的類型。 例如,下面的例子展示了如何添加一個intent filter來激活你的[activity](# "An activity represents a single screen with a user interface."): ~~~ <activity android:name="com.example.android.nfctransfer.ViewActivity" android:label="Android Beam Viewer" > ... <intent-filter> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.DEFAULT"/> ... </intent-filter> </activity> ~~~ > **Note:**不僅僅只有Android Beam文件傳輸會發送含有[ACTION_VIEW](http://developer.android.com/reference/android/content/Intent.html#ACTION_VIEW)這一Action的Intent。在接收設備上的其它應用也有可能會發送含有該Action的intent。我們馬上會進一步討論這一問題。 ### 請求文件讀權限 如果要讀取Android Beam文件傳輸所拷貝到設備上的文件,需要[READ_EXTERNAL_STORAGE](http://developer.android.com/reference/android/Manifest.permission.html#READ_EXTERNAL_STORAGE)權限。例如: ~~~ <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> ~~~ 如果你希望將文件拷貝至應用程序自己的存儲區,那么需要的權限改為[WRITE_EXTERNAL_STORAGE](http://developer.android.com/reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE),另外[WRITE_EXTERNAL_STORAGE](http://developer.android.com/reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE)權限包含了[READ_EXTERNAL_STORAGE](http://developer.android.com/reference/android/Manifest.permission.html#READ_EXTERNAL_STORAGE)權限。 > **Note:**對于Android 4.2.2(API Level 17)及之前版本的系統,[READ_EXTERNAL_STORAGE](http://developer.android.com/reference/android/Manifest.permission.html#READ_EXTERNAL_STORAGE)權限僅在用戶選擇要讀文件時才是強制需要的。而在今后的版本中會在所有情況下都需要該權限。為了保證未來應用程序在的穩定性,建議在Manifest清單文件中聲明該權限。 由于你的應用對于其自身的內部存儲區域具有控制權,所以若要將文件拷貝至應用程序自身的的內部存儲區域,寫權限是不需要聲明的。 ### 獲取拷貝文件的目錄 Android Beam文件傳輸一次性將所有文件拷貝到目標設備的一個目錄中,Android Beam文件傳輸通知所發出的[Intent](http://developer.android.com/reference/android/content/Intent.html)中包含有URI,該URI指向了第一個被傳輸的文件。然而,你的應用程序也有可能接收到除了Android Beam文件傳輸之外的某個來源所發出的含有[ACTION_VIEW](http://developer.android.com/reference/android/content/Intent.html#ACTION_VIEW)這一Action的Intent。為了明確你應該如何處理接收的Intent,你需要檢查它的Scheme和Authority。 可以調用[Uri.getScheme()](http://developer.android.com/reference/android/net/Uri.html#getScheme())獲得URI的Scheme,下面的代碼展示了如何確定Scheme并對URI進行相應的處理: ~~~ public class MainActivity extends Activity { ... // A File object containing the path to the transferred files private File mParentPath; // Incoming Intent private Intent mIntent; ... /* * Called from onNewIntent() for a SINGLE_TOP Activity * or onCreate() for a new Activity. For onNewIntent(), * remember to call setIntent() to store the most * current Intent * */ private void handleViewIntent() { ... // Get the Intent action mIntent = getIntent(); String action = mIntent.getAction(); /* * For ACTION_VIEW, the Activity is being asked to display data. * Get the URI. */ if (TextUtils.equals(action, Intent.ACTION_VIEW)) { // Get the URI from the Intent Uri beamUri = mIntent.getData(); /* * Test for the type of URI, by getting its scheme value */ if (TextUtils.equals(beamUri.getScheme(), "file")) { mParentPath = handleFileUri(beamUri); } else if (TextUtils.equals( beamUri.getScheme(), "content")) { mParentPath = handleContentUri(beamUri); } } ... } ... } ~~~ ### 從File URI中獲取目錄 如果接收的[Intent](http://developer.android.com/reference/android/content/Intent.html)包含一個File URI,則該URI包含了一個文件的絕對文件名,它包括了完整的路徑和文件名。對于Android Beam文件傳輸來說,目錄路徑指向了其它被傳輸文件的位置(如果有其它傳輸文件的話),要獲得這個目錄路徑,需要取得URI的路徑部分(URI中除去“file:”前綴的部分),根據路徑創建一個[File](http://developer.android.com/reference/java/io/File.html)對象,然后獲取這個[File](http://developer.android.com/reference/java/io/File.html)的父目錄: ~~~ ... public String handleFileUri(Uri beamUri) { // Get the path part of the URI String fileName = beamUri.getPath(); // Create a File object for this filename File copiedFile = new File(fileName); // Get a string containing the file's parent directory return copiedFile.getParent(); } ... ~~~ ### 從Content URI獲取目錄 如果接收的[Intent](http://developer.android.com/reference/android/content/Intent.html)包含一個Content URI,這個URI可能指向的是存儲于[MediaStore](http://developer.android.com/reference/android/provider/MediaStore.html) Content Provider的目錄和文件名。你可以通過檢測URI的Authority值來判斷它是否是來自于[MediaStore](http://developer.android.com/reference/android/provider/MediaStore.html)的Content URI。一個[MediaStore](http://developer.android.com/reference/android/provider/MediaStore.html)的Content URI可能來自Android Beam文件傳輸也可能來自其它應用程序,但不管怎么樣,你都能根據該Content URI獲得一個目錄路徑和文件名。 你也可以接收一個含有[ACTION_VIEW](http://developer.android.com/reference/android/content/Intent.html#ACTION_VIEW)這一Action的Intent,它包含的Content URI針對于Content Provider,而不是[MediaStore](http://developer.android.com/reference/android/provider/MediaStore.html),在這種情況下,這個Content URI不包含[MediaStore](http://developer.android.com/reference/android/provider/MediaStore.html)的Authority,且這個URI一般不指向一個目錄。 > **Note:**對于Android Beam文件傳輸,接收在含有[ACTION_VIEW](http://developer.android.com/reference/android/content/Intent.html#ACTION_VIEW)的Intent中的Content URI時,如果第一個接收的文件,其MIME類型為“audio/_”,“image/_”或者“video/*”,Android Beam文件傳輸會在它存儲傳輸文件的目錄內運行Media Scanner,以此為媒體文件添加索引。同時Media Scanner將結果寫入[MediaStore](http://developer.android.com/reference/android/provider/MediaStore.html)的Content Provider,之后它將第一個文件的Content URI回遞給Android Beam文件傳輸。這個Content URI就是你在通知[Intent](http://developer.android.com/reference/android/content/Intent.html)中所接收到的。要獲得第一個文件的目錄,你需要使用該Content URI從[MediaStore](http://developer.android.com/reference/android/provider/MediaStore.html)中獲取它。 ### 確定Content Provider 為了明確你能從Content URI中獲取文件目錄,你可以通過調用[Uri.getAuthority()](http://developer.android.com/reference/android/net/Uri.html#getAuthority())獲取URI的Authority,以此確定與該URI相關聯的Content Provider。其結果有兩個可能的值: **[MediaStore.AUTHORITY](http://developer.android.com/reference/android/provider/MediaStore.html#AUTHORITY)** 表明這個URI關聯了被[MediaStore](http://developer.android.com/reference/android/provider/MediaStore.html)記錄的一個文件或者多個文件。可以從[MediaStore](http://developer.android.com/reference/android/provider/MediaStore.html)中獲取文件的全名,目錄名就自然可以從文件全名中獲取。 **其他值** 來自其他Content Provider的Content URI。可以顯示與該Content URI相關聯的數據,但是不要嘗試去獲取文件目錄。 要從[MediaStore](http://developer.android.com/reference/android/provider/MediaStore.html)的Content URI中獲取目錄,我們需要執行一個查詢操作,它將[Uri](http://developer.android.com/reference/android/net/Uri.html)參數指定為收到的ContentURI,將[MediaColumns.DATA](http://developer.android.com/reference/android/provider/MediaStore.MediaColumns.html#DATA)列作為投影(Projection)。返回的[Cursor](http://developer.android.com/reference/android/database/Cursor.html)對象包含了URI所代表的文件的完整路徑和文件名。該目錄路徑下還包含了由Android Beam文件傳輸傳送到該設備上的其它文件。 下面的代碼展示了你要如何測試Content URI的Authority,并獲取傳輸文件的路徑和文件名: ~~~ ... public String handleContentUri(Uri beamUri) { // Position of the filename in the query Cursor int filenameIndex; // File object for the filename File copiedFile; // The filename stored in MediaStore String fileName; // Test the authority of the URI if (!TextUtils.equals(beamUri.getAuthority(), MediaStore.AUTHORITY)) { /* * Handle content URIs for other content providers */ // For a MediaStore content URI } else { // Get the column that contains the file name String[] projection = { MediaStore.MediaColumns.DATA }; Cursor pathCursor = getContentResolver().query(beamUri, projection, null, null, null); // Check for a valid cursor if (pathCursor != null && pathCursor.moveToFirst()) { // Get the column index in the Cursor filenameIndex = pathCursor.getColumnIndex( MediaStore.MediaColumns.DATA); // Get the full file name including path fileName = pathCursor.getString(filenameIndex); // Create a File object for the filename copiedFile = new File(fileName); // Return the parent directory of the file return new File(copiedFile.getParent()); } else { // The query didn't work; return null return null; } } } ... ~~~ 要學習更多關于從Content Provider獲取數據的知識,可以閱讀:[Retrieving Data from the Provider](http://developer.android.com/guide/topics/providers/content-provider-basics.html#SimpleQuery)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看