android developer tiny share-20160823

今天讲使用intent实现文件的读写。内容较多,大概了解下。

File Storage
Retrieve a specific type of file

To request that the user select a file such as a document or photo and return a reference to your app, use the ACTION_GET_CONTENT action and specify your desired MIME type. The file reference returned to your app is transient to your activity's current lifecycle, so if you want to access it later you must import a copy that you can read later. This intent also allows the user to create a new file in the process (for example, instead of selecting an existing photo, the user can capture a new photo with the camera).

The result intent delivered to your onActivityResult() method includes data with a URI pointing to the file. The URI could be anything, such as an http: URI, file: URI, or content: URI. However, if you'd like to restrict selectable files to only those that are accessible from a content provider (a content: URI) and that are available as a file stream with openFileDescriptor(), you should add the CATEGORY_OPENABLE category to your intent.

On Android 4.3 (API level 18) and higher, you can also allow the user to select multiple files by adding EXTRA_ALLOW_MULTIPLE to the intent, set to true. You can then access each of the selected files in a ClipData object returned by getClipData().

Action
    ACTION_GET_CONTENT
Data URI Scheme
    None
MIME Type
    The MIME type corresponding to the file type the user should select.
Extras
    EXTRA_ALLOW_MULTIPLE
        A boolean declaring whether the user can select more than one file at a time.
    EXTRA_LOCAL_ONLY
        A boolean that declares whether the returned file must be available directly from the device, rather than requiring a download from a remote service.
Category (optional)
    CATEGORY_OPENABLE
To return only "openable" files that can be represented as a file stream with openFileDescriptor().
Example intent to get a photo:

static final int REQUEST_IMAGE_GET = 1;

public void selectImage() {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("image/*");
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(intent, REQUEST_IMAGE_GET);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_GET && resultCode == RESULT_OK) {
        Bitmap thumbnail = data.getParcelable("data");
        Uri fullPhotoUri = data.getData();
        // Do work with photo saved at fullPhotoUri
        ...
    }
}

Example intent filter to return a photo:

<activity ...>
    <intent-filter>
        <action android:name="android.intent.action.GET_CONTENT" />
        <data android:type="image/*" />
        <category android:name="android.intent.category.DEFAULT" />
        <!-- The OPENABLE category declares that the returned file is accessible
             from a content provider that supports OpenableColumns
             and ContentResolver.openFileDescriptor() -->
        <category android:name="android.intent.category.OPENABLE" />
    </intent-filter>
</activity>


Open a specific type of file
Instead of retrieving a copy of a file that you must import to your app (by using the ACTION_GET_CONTENT action), when running on Android 4.4 or higher, you can instead request to open a file that's managed by another app by using the ACTION_OPEN_DOCUMENT action and specifying a MIME type. To also allow the user to instead create a new document that your app can write to, use the ACTION_CREATE_DOCUMENT action instead. For example, instead of selecting from existing PDF documents, the ACTION_CREATE_DOCUMENT intent allows users to select where they'd like to create a new document (within another app that manages the document's storage)—your app then receives the URI location of where it can write the new document.

Whereas the intent delivered to your onActivityResult() method from the ACTION_GET_CONTENT action may return a URI of any type, the result intent from ACTION_OPEN_DOCUMENT and ACTION_CREATE_DOCUMENT always specify the chosen file as a content: URI that's backed by a DocumentsProvider. You can open the file with openFileDescriptor() and query its details using columns from DocumentsContract.Document.

The returned URI grants your app long-term read access to the file (also possibly with write access). So the ACTION_OPEN_DOCUMENT action is particularly useful (instead of using ACTION_GET_CONTENT) when you want to read an existing file without making a copy into your app, or when you want to open and edit a file in place.

You can also allow the user to select multiple files by adding EXTRA_ALLOW_MULTIPLE to the intent, set to true. If the user selects just one item, then you can retrieve the item from getData(). If the user selects more than one item, then getData() returns null and you must instead retrieve each item from a ClipData object that is returned by getClipData().

Note: Your intent must specify a MIME type and must declare the CATEGORY_OPENABLE category. If appropriate, you can specify more than one MIME type by adding an array of MIME types with the EXTRA_MIME_TYPES extra—if you do so, you must set the primary MIME type in setType() to "*/*".

Action
    ACTION_OPEN_DOCUMENT or
    ACTION_CREATE_DOCUMENT
Data URI Scheme
    None
MIME Type
    The MIME type corresponding to the file type the user should select.
Extras
    EXTRA_MIME_TYPES
        An array of MIME types corresponding to the types of files your app is requesting. When you use this extra, you must set the primary MIME type in setType() to "*/*".
    EXTRA_ALLOW_MULTIPLE
        A boolean that declares whether the user can select more than one file at a time.
    EXTRA_TITLE
        For use with ACTION_CREATE_DOCUMENT to specify an initial file name.
    EXTRA_LOCAL_ONLY
        A boolean that declares whether the returned file must be available directly from the device, rather than requiring a download from a remote service.
Category
    CATEGORY_OPENABLE
        To return only "openable" files that can be represented as a file stream with openFileDescriptor().
Example intent to get a photo:

static final int REQUEST_IMAGE_OPEN = 1;

public void selectImage() {
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.setType("image/*");
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    // Only the system receives the ACTION_OPEN_DOCUMENT, so no need to test.
    startActivityForResult(intent, REQUEST_IMAGE_OPEN);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_OPEN && resultCode == RESULT_OK) {
        Uri fullPhotoUri = data.getData();
        // Do work with full size photo saved at fullPhotoUri
        ...
    }
}

Third party apps cannot actually respond to an intent with the ACTION_OPEN_DOCUMENT action. Instead, the system receives this intent and displays all the files available from various apps in a unified user interface.

To provide your app's files in this UI and allow other apps to open them, you must implement a DocumentsProvider and include an intent filter for PROVIDER_INTERFACE ("android.content.action.DOCUMENTS_PROVIDER"). For example:

<provider ...
    android:grantUriPermissions="true"
    android:exported="true"
    android:permission="android.permission.MANAGE_DOCUMENTS">
    <intent-filter>
        <action android:name="android.content.action.DOCUMENTS_PROVIDER" />
    </intent-filter>
</provider>

For more information about how to make the files managed by your app openable from other apps, read the Storage Access Framework guide.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值