File Association for Android applications

Source: Internet
Author: User

To implement File Association, first modify androidmanifest. xml. The androidmanifest example is as follows:

 <? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "com. android. sample "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> <br/> <application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name" <br/> Android: debuggable = "true"> <br/> <activity Android: name = ". mainactivity "Android: configchanges =" orientation | keyboardhidden | navigation "Android: label = "@ string/app_name"> <br/> <intent-filter> <br/> <action Android: Name = "android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> <br/> <activity Android: Name = ". otheractivity "Android: configchanges =" orientation | keyboardhidden | navigation "/> <br/> <activity Android: Name = ". associatedactivity "Android: configchanges =" orientation | keyboardhidden | navigation "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. view "/> <br/> <category Android: Name =" android. intent. category. default "/> <br/> <data Android: mimetype =" text/plain "> </data> <br/> <data Android: mimetype = "application/Epub + Zip"> </data> <br/> </intent-filter> <br/> </activity> <br/> </Application> <br/> <uses-SDK Android: minsdkversion = "8"/> <br/> <uses-Permission Android: Name = "android. permission. write_external_storage "/> <br/> <uses-Permission Android: Name =" android. permission. internet "/> <br/> </manifest>

 

In this example, there are two "intent-filter", the first is the first activity started by the application, and the second "intent-filter" is used to implement File Association.
<Intent-filter>
<Action Android: Name = "android. Intent. Action. View"/>
<Category Android: Name = "android. Intent. Category. Default"/>
<Data Android: mimetype = "text/plain"> </data>
<Data Android: mimetype = "application/Epub + Zip"> </data>
</Intent-filter>

In this XML section, the program is registered to be associated with the TXT and Epub file formats. If JPEG is to be associated, mimetype is changed to: "image/JPEG", and the association with all files is changed: "*/*". in this way, when you click related files in the File Manager, the system will attempt to execute your program.

 

Public class associatedactivity extends activity {</P> <p>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> intent = getintent (); <br/> string action = intent. getaction (); <br/> If (intent. action_view.equals (Action) {<br/> URI uri = (URI) intent. getdata (); <br/> string filename = Uri. getpath (); <br/> log. E ("info", filename); <br/>}< br/>}

 

Here, the action mainly determines whether it comes from the view action, which corresponds to Android. Intent. Action. View in XML. You can use intent. getdata to obtain the passed parameters, that is, the clicked file.

For more information about MIME type, see http://en.wikipedia.org/wiki/MIME_type.

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.