Android如何請求分享一個文件

2018-08-02 17:30 更新

編寫:jdneo - 原文:http://developer.android.com/training/secure-file-sharing/request-file.html

當一個應(yīng)用程序希望訪問由其它應(yīng)用程序所共享的文件時,請求應(yīng)用程序(客戶端)經(jīng)常會向其它應(yīng)用程序(服務(wù)端)發(fā)送一個文件請求。多數(shù)情況下,該請求會導(dǎo)致在服務(wù)端應(yīng)用程序中啟動一個Activity,該Activity中會顯示可以共享的文件。當服務(wù)端應(yīng)用程序向客戶端應(yīng)用程序返回了文件的Content URI后,用戶即可開始選擇文件。

本課將展示一個客戶端應(yīng)用程序應(yīng)該如何向服務(wù)端應(yīng)用程序請求一個文件,接收服務(wù)端應(yīng)用程序發(fā)來的Content URI,然后使用這個Content URI打開這個文件。

發(fā)送一個文件請求

為了向服務(wù)端應(yīng)用程序發(fā)送文件請求,在客戶端應(yīng)用程序中,需要調(diào)用startActivityForResult)方法,同時傳遞給這個方法一個Intent參數(shù),它包含了客戶端應(yīng)用程序能處理的某個Action,比如ACTION_PICK及一個MIME類型。

例如,下面的代碼展示了如何向服務(wù)端應(yīng)用程序發(fā)送一個Intent,來啟動在分享文件中提到的Activity

public class MainActivity extends Activity {
    private Intent mRequestFileIntent;
    private ParcelFileDescriptor mInputPFD;
    ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRequestFileIntent = new Intent(Intent.ACTION_PICK);
        mRequestFileIntent.setType("image/jpg");
        ...
    }
    ...
    protected void requestFile() {
        /**
         * When the user requests a file, send an Intent to the
         * server app.
         * files.
         */
            startActivityForResult(mRequestFileIntent, 0);
        ...
    }
    ...
}

訪問請求的文件

當服務(wù)端應(yīng)用程序向客戶端應(yīng)用程序發(fā)回包含Content URI的Intent時,該Intent會傳遞給客戶端應(yīng)用程序重寫的onActivityResult()方法當中。一旦客戶端應(yīng)用程序擁有了文件的Content URI,它就可以通過獲取其FileDescriptor訪問文件了。

這一過程中不用過多擔心文件的安全問題,因為客戶端應(yīng)用程序所收到的所有數(shù)據(jù)只有文件的Content URI而已。由于URI不包含目錄路徑信息,客戶端應(yīng)用程序無法查詢或打開任何服務(wù)端應(yīng)用程序的其他文件??蛻舳藨?yīng)用程序僅僅獲取了這個文件的訪問渠道以及由服務(wù)端應(yīng)用程序授予的訪問權(quán)限。同時訪問權(quán)限是臨時的,一旦這個客戶端應(yīng)用的任務(wù)棧結(jié)束了,這個文件將無法再被除服務(wù)端應(yīng)用程序之外的其他應(yīng)用程序訪問。

下面的例子展示了客戶端應(yīng)用程序應(yīng)該如何處理發(fā)自服務(wù)端應(yīng)用程序的Intent,以及客戶端應(yīng)用程序如何使用Content URI獲取FileDescriptor

    /*
     * When the Activity of the app that hosts files sets a result and calls
     * finish(), this method is invoked. The returned Intent contains the
     * content URI of a selected file. The result code indicates if the
     * selection worked or not.
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode,
            Intent returnIntent) {
        // If the selection didn't work
        if (resultCode != RESULT_OK) {
            // Exit without doing anything else
            return;
        } else {
            // Get the file's content URI from the incoming Intent
            Uri returnUri = returnIntent.getData();
            /*
             * Try to open the file for "read" access using the
             * returned URI. If the file isn't found, write to the
             * error log and return.
             */
            try {
                /*
                 * Get the content resolver instance for this context, and use it
                 * to get a ParcelFileDescriptor for the file.
                 */
                mInputPFD = getContentResolver().openFileDescriptor(returnUri, "r");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                Log.e("MainActivity", "File not found.");
                return;
            }
            // Get a regular file descriptor for the file
            FileDescriptor fd = mInputPFD.getFileDescriptor();
            ...
        }
    }

openFileDescriptor()方法返回一個文件的ParcelFileDescriptor對象??蛻舳藨?yīng)用程序從該對象中獲取FileDescriptor對象,然后利用該對象讀取這個文件了。


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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號