Android接收其他設(shè)備的文件

2018-08-02 17:32 更新

編寫(xiě):jdneo - 原文:http://developer.android.com/training/beam-files/receive-files.html

Android Beam文件傳輸將文件拷貝至接收設(shè)備上的某個(gè)特殊目錄。同時(shí)使用Android Media Scanner掃描拷貝的文件,并在MediaStore provider中為媒體文件添加對(duì)應(yīng)的條目記錄。本課將展示當(dāng)文件拷貝完成時(shí)要如何響應(yīng),以及在接收設(shè)備上應(yīng)該如何定位拷貝的文件。

響應(yīng)請(qǐng)求并顯示數(shù)據(jù)

當(dāng)Android Beam文件傳輸將文件拷貝至接收設(shè)備后,它會(huì)發(fā)布一個(gè)包含Intent的通知,該Intent擁有:ACTION_VIEW,首個(gè)被傳輸文件的MIME類(lèi)型,以及一個(gè)指向第一個(gè)文件的URI。用戶(hù)點(diǎn)擊該通知后,Intent會(huì)被發(fā)送至系統(tǒng)。為了使我們的應(yīng)用程序能夠響應(yīng)該Intent,我們需要為響應(yīng)的Activity所對(duì)應(yīng)的<activity>標(biāo)簽添加一個(gè)<intent-filter>標(biāo)簽,在<intent-filter>標(biāo)簽中,添加以下子標(biāo)簽:

<action android:name="android.intent.action.VIEW" />

該標(biāo)簽用來(lái)匹配從通知發(fā)出的Intent,這些Intent具有ACTION_VIEW這一Action。

<category android:name="android.intent.category.CATEGORY_DEFAULT" />

該標(biāo)簽用來(lái)匹配不含有顯式Category的Intent對(duì)象。

<data android:mimeType="mime-type" />

該標(biāo)簽用來(lái)匹配一個(gè)MIME類(lèi)型。僅僅指定那些我們的應(yīng)用能夠處理的類(lèi)型。

下例展示了如何添加一個(gè)intent filter來(lái)激活我們的activity:

    <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的Intent的唯一可能發(fā)送者。在接收設(shè)備上的其它應(yīng)用也有可能會(huì)發(fā)送含有該Action的intent。我們馬上會(huì)進(jìn)一步討論這一問(wèn)題。

請(qǐng)求文件讀權(quán)限

要讀取Android Beam文件傳輸所拷貝到設(shè)備上的文件,需要請(qǐng)求READ_EXTERNAL_STORAGE權(quán)限。例如:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

如果希望將文件拷貝至應(yīng)用程序自己的存儲(chǔ)區(qū),那么需要的權(quán)限改為WRITE_EXTERNAL_STORAGE,另外,WRITE_EXTERNAL_STORAGE權(quán)限包含了READ_EXTERNAL_STORAGE權(quán)限。

Note:對(duì)于Android 4.2.2(API Level 17)及之前版本的系統(tǒng),READ_EXTERNAL_STORAGE權(quán)限僅在用戶(hù)選擇要讀文件時(shí)才是強(qiáng)制需要的。而在今后的版本中會(huì)在所有情況下都需要該權(quán)限。為保證應(yīng)用程序在未來(lái)的穩(wěn)定性,建議在Manifest清單文件中聲明該權(quán)限。

由于我們的應(yīng)用對(duì)于自身的內(nèi)部存儲(chǔ)區(qū)域具有控制權(quán),因此當(dāng)要將文件拷貝至應(yīng)用程序自身的的內(nèi)部存儲(chǔ)區(qū)域時(shí),不需要聲明寫(xiě)權(quán)限。

獲取拷貝文件的目錄

Android Beam文件傳輸一次性將所有文件拷貝到目標(biāo)設(shè)備的一個(gè)目錄中,Android Beam文件傳輸通知所發(fā)出的Intent中含有指向了第一個(gè)被傳輸?shù)奈募腢RI。然而,我們的應(yīng)用程序也有可能接收到除了Android Beam文件傳輸之外的某個(gè)來(lái)源所發(fā)出的含有ACTION_VIEW這一Action的Intent。為了明確應(yīng)該如何處理接收的Intent,我們要檢查它的Scheme和Authority。

可以調(diào)用Uri.getScheme()獲得URI的Scheme,下例展示了如何確定Scheme并對(duì)URI進(jìn)行相應(yīng)的處理:

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包含一個(gè)File URI,則該URI包含了一個(gè)文件的絕對(duì)文件名,它包括了完整的路徑和文件名。對(duì)Android Beam文件傳輸來(lái)說(shuō),目錄路徑指向了其它被傳輸文件的位置(如果有其它傳輸文件的話),要獲得該目錄路徑,需要取得URI的路徑部分(URI中除去“file:”前綴的部分),根據(jù)路徑創(chuàng)建一個(gè)File對(duì)象,然后獲取這個(gè)File的父目錄:

    ...
    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包含一個(gè)Content URI,這個(gè)URI可能指向的是存儲(chǔ)于MediaStore Content Provider的目錄和文件名。我們可以通過(guò)檢測(cè)URI的Authority值來(lái)判斷它是否是來(lái)自于MediaStore的Content URI。一個(gè)MediaStore的Content URI可能來(lái)自Android Beam文件傳輸也可能來(lái)自其它應(yīng)用程序,但不管怎么樣,我們都能根據(jù)該Content URI獲得一個(gè)目錄路徑和文件名。

我們也可以接收一個(gè)含有ACTION_VIEW這一Action的Intent,它包含的Content URI針對(duì)于Content Provider,而不是MediaStore,這種情況下,該Content URI不包含MediaStore的Authority,且這個(gè)URI一般不指向一個(gè)目錄。

Note:對(duì)于Android Beam文件傳輸,接收在含有ACTION_VIEW的Intent中的Content URI時(shí),若第一個(gè)接收的文件MIME類(lèi)型為“audio/”,“image/”或者“video/*”,Android Beam文件傳輸會(huì)在它存儲(chǔ)傳輸文件的目錄內(nèi)運(yùn)行Media Scanner,以此為媒體文件添加索引。同時(shí)Media Scanner將結(jié)果寫(xiě)入MediaStore的Content Provider,之后它將第一個(gè)文件的Content URI回遞給Android Beam文件傳輸。這個(gè)Content URI就是我們?cè)谕ㄖ?a rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" target="_blank">Intent中所接收到的。要獲得第一個(gè)文件的目錄,需要使用該Content URI從MediaStore中獲取它。

確定Content Provider

為了確定是否能從Content URI中獲取文件目錄,可以通過(guò)調(diào)用Uri.getAuthority()獲取URI的Authority,以此確定與該URI相關(guān)聯(lián)的Content Provider。其結(jié)果有兩個(gè)可能的值:

MediaStore.AUTHORITY

表明該URI關(guān)聯(lián)了被MediaStore記錄的一個(gè)文件或者多個(gè)文件??梢詮?a rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" target="_blank">MediaStore中獲取文件的全名,目錄名就自然可以從文件全名中獲取。

其他值

來(lái)自其他Content Provider的Content URI。可以顯示與該Content URI相關(guān)聯(lián)的數(shù)據(jù),但是不要嘗試去獲取文件目錄。

要從MediaStore的Content URI中獲取目錄,我們需要執(zhí)行一個(gè)查詢(xún)操作,它將Uri參數(shù)指定為收到的ContentURI,將MediaColumns.DATA列作為投影(Projection)。返回的Cursor對(duì)象包含了URI所代表的文件的完整路徑和文件名。該目錄路徑下還包含了由Android Beam文件傳輸傳送到該設(shè)備上的其它文件。

下面的代碼展示了如何測(cè)試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;
             }
        }
    }
    ...

更多關(guān)于從Content Provider獲取數(shù)據(jù)的知識(shí),請(qǐng)參考:Retrieving Data from the Provider。


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)