Android text and input --- copy and paste

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 stored 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 it to copy data streams: 1. Create a content URI corresponding to the data streams to be placed on the clipboard. Including the following optional content:. 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 this 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 () is a more general method than the openTypeAssetFile () method. It does not filter the MIME type, but can read the content of the file. OpenFile () is more generalized than openAssetFile. 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. This is what we did in the Note Pad example. For details, see the openTypeAssetFile () method of NotePadProvider. java. To design a valid 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. Share to: Previous Article: Android text and input --- copy and paste (5)

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.