Android text and input-copy and paste (6)

Source: Internet
Author: User

Copy streaming data

You can copy and paste streaming large text and binary data. This data can be in the following format:

1. files saved on the actual device;

2. Data streams from sockets;

3. Big data stored in the underlying database system.

The content provider for data streams uses a file description object (such as assetfiledescriptor, rather than a cursor object) to provide access to data. Applications that paste data use this file description object to read data streams.

Follow these steps to create an application that provides data stream replication:

1. Create a content URI corresponding to the data stream to be placed on the clipboard. It includes the following optional items:

A. Compile an identifier for the data stream to be put on the URI, and maintain a table in the provider that contains the identifier and the corresponding data stream name;

B. Write the data stream name directly to the URI;

C. Use a unique URI that can always return the current data stream from the provider. If you choose this method, you must remember that whenever you copy a data stream to the Clipboard through Uri, you must update your provider to point to different data streams.

2. Each data stream you plan to provide provides a MIME type. Applications that paste data need this information to determine whether they can paste data on the clipboard.

3. Implement a method in the contentprovider class and let it return the file description object of the corresponding data stream. If you write the identifier to the URI of the content, you can use this method to determine whether the corresponding data stream is opened.

4. to copy the data stream to the clipboard, You need to construct the URI of the content and put it on the clipboard.

To paste a data stream, the application obtains the clipping object and its URI from the clipboard and uses it in the method that calls the contentresolver object to open the data stream file description object. The contentresolver method calls the corresponding contentprovider method and passes the content URI to it. You provide a method to return the description object of this file to contentresolver. Then the application that pasted the data can read the data from the data stream.

The following describes the object description methods that are most important to the content provider. Each method has a corresponding method name appended with the descriptor string in the contentresolver object. For example, the openassetfile () method corresponding to the contentreslolver method is the openassetfiledescriptor () method.

Opentypeassetfile ()

If the content provider supports the provided MIME type, this method returns a description object of the resource file. The caller application provides a MIME type mode. If the content provider can provide this mime type, it will return an assetfiledescriptor file handle, otherwise it will throw an exception.

This method can process the file content. You can use it to read resources that have been copied to the clipboard by the content provider.

Openassetfile ()

This method is more general than the opentypeassetfile () method. It does not filter the MIME type, but can read the content of the file.

Openfile ()

This method is more general than the openassetfile () method. It cannot file content.

You can choose to use the openpipehelper () method of the alternative file descriptor method, which allows applications that paste data to use pipelines in background threads to read streaming data. To use this method, you need to implement the contentprovider. pipedatawriter interface. In note
This is what we did in the pad example. For details, see the opentypeassetfile () method of notepadprovider. java.

Design valid copy/Paste functions

To design an effective copy/paste function for your application, remember the following:

1. At any time, there is only one clipboard object. In the system, the copy operation of any new application will overwrite the previous clipboard object. Because the user leaves your application after the copy operation, you cannot assume that the Clipboard contains the clipboard object that was previously copied in your application.

2. Multiple clipdata. Item objects in each cut object are used to copy and paste multiple selected objects, rather than apply different forms to the same selected object. Generally, all the clipdata. Item objects in the clipboard object must have the same format. That is to say, they are applied to simple text, content Uri, or intent objects, rather than mixed objects.

3. You can provide different mime descriptions when providing data. Add the MIME type to the clipdescription object and implement the MIME type in your content provider.

4. when you obtain data from the clipboard, your application is responsible for checking whether the MIME type is available. If it is available, then you decide to use that type. Otherwise, even if there is a clipboard object, in addition, if you request the paste operation, your application will not perform the paste operation. If the MIME type is compatible, you must paste it. You can use the coercetotext () method to forcibly convert the data on the clipboard to text. If your application supports multiple types of available MIME types, you can allow users to choose which type to use.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.