Android footstep---itent.action_pick, startactivityforresult

Source: Internet
Author: User

 Public voidOnClick (View v) {//TODO auto-generated Method StubIntent Intent =NewIntent (Intent.action_pick); Intent.settype ("Image/*"); Switch(V.getid ()) { CaseR.id.btfirst:startactivityforresult (Intent,1);  Break;  CaseR.id.btsecond:startactivityforresult (Intent,2);  Break;  CaseR.id.btdetect:FinalString Firstpath =Etfirst.gettext (). toString (); FinalString Secondpath = Etsecond.gettext (). toString ();

Then rewrite the method, corresponding to:

@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {        //TODO auto-generated Method Stub        Super. Onactivityresult (Requestcode, ResultCode, data); if(Data! =NULL) {cursor cursor= Getcontentresolver (). Query (Data.getdata (),NULL,                    NULL,NULL,NULL);            Cursor.movetofirst (); intIDX =Cursor.getcolumnindex (Imagecolumns.data); String filesrc=cursor.getstring (IDX); if(Requestcode = = 1) {etfirst.settext (FILESRC); } Else if(Requestcode = = 2) {etsecond.settext (FILESRC);        } cursor.close (); }    }

Cursor cursor Gets the database pointer cursor, pointing to Data.getdata, like a section of storage, Cursor.movetofirst (); point to the first store, int idx = Cursor.getcolumnindex ( Imagecolumns.data); Idx is quite an index, pointing (Imagecolumns.data,

Requestcode is important, Startactivityforresult (Intent Intent, int requestcode) The requestcode that correspond to the two parameters in Startactivityforresult,

http://blog.csdn.net/chenzheng_java/article/details/6266135

In code: Intent Intent = new Intent (Intent.action_pick);

Android Itent.action_pick Intent.action_get_content Magical

Do you often want to find someone in the list of pop-up calls, and then get more information about that person?

Do you want to choose a picture from the list that pops up and then do it further?

If you want to, then you are not very much like to know, how should we let it pop out a list of choices, and how to implement the code behind the operation?

Itent.action_pick Intent.action_get_content Both can do similar functions, let's take a look at the example

First: Intent.action_pick

First add a permission: <uses-permission android:name= "Android.permission.READ_CONTACTS"/> Initiates a contact pickerintent intent = New Intent (Intent.action_pick, People.content_uri); Startactivityforresult (Intent, pick_contact); then: Override method
@Override Public voidOnactivityresult (intReqcode,intResultCode, Intent data) {        Super. Onactivityresult (Reqcode, ResultCode, data); Switch(reqcode) { Case(pick_contact):if(ResultCode = =ACTIVITY.RESULT_OK) {Uri Contactdata=Data.getdata (); Cursor C= Managedquery (Contactdata,NULL,NULL,NULL,NULL); if(C.movetofirst ()) {String name=c.getstring (C.getcolumnindexorthrow (people.name)); //TODO Whatever you want to does with the selected contact name.                                  }                            }                            Break; }    } 

where cursor is the knowledge of the database, equivalent to a pointer in the database, query gets Contactdata,
For example

Newintnewintnew simplecursoradapter ( this

Second: intent.action_get_contentWe can see that in fact action_get_content is determined by the Type property set in intent to determine which program to invoke.
    1. Intent Intent = new Intent (intent.action_get_content);
    2. Intent.settype ("audio/*");
    3. StartActivity (Intent.createchooser (Intent, "select Music"));
[Java]View Plaincopyprint?
    1. <span style= "Font-family:comic sans ms,sans-serif;" ><span style= "Font-size:x-small;" >intent Intent = new Intent (intent.action_get_content);
    2. Intent.settype ("audio/*");
    3. StartActivity (Intent.createchooser (Intent, "select Music"));</span></span>
Intent Intent = new Intent (intent.action_get_content); Intent.settype ("audio/*"); StartActivity (Intent.createchooser (Intent, "select Music");

The execution will pop up a dialog box with the effect:

In fact, for this piece of code, we should all be able to guess what the meaning is to simulate and understand

Code

1. Define the testactivity used to play the target based on the incoming URI

<span style= "Font-family:comic sans ms,sans-serif;" ><span style= "Font-size:x-small;" > Public classTestactivityextendsActivity {@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main);  This. Settitle ("Testactivity"); Intent I= This. Getintent (); Uri u=I.getdata (); Try{playmusic (U); } Catch(IllegalArgumentException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(SecurityException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IllegalStateException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }         Public voidPlaymusic (URI Uri)throwsillegalargumentexception, SecurityException, IllegalStateException, ioexception{MediaPlayer MP=NewMediaPlayer (); Mp.setdatasource ( This, URI);        Mp.prepare ();    Mp.start (); }}</span></span>

2. Register at Androidmanifest testactivity

<span style= "Font-family:comic sans ms,sans-serif;" ><span style= "Font-size:x-small;" ><activity android:name= ". Testactivity "                  android:label=" Testactivity ">            <intent-filter>                <action android: Name= "Android.intent.action.GET_CONTENT"/>                 <category android:name= "Android.intent.category.DEFAULT"/ >                 <category android:name= "Android.intent.category.OPENABLE"/>                 <data android:mimetype= "audio/ Music1 "/>            </intent-filter>        </activity></span></span>

3. Using testactivity

<span style= "Font-family:comic sans ms,sans-serif;" ><span style= "Font-size:x-small;" >publicvoid  Sendchooser () {        new  Intent (intent.action_get_content);                Intent.setdataandtype (Uri.parse ("File:///sdcard/DCIM/cc.mp3"), "Audio/music1");        " Select Music1 App "));    } </span></span>

4. Emulator operation:

In addition

   //Select the identity returned by the picture RequestcodeIntent innerintent=NewIntent (intent.action_get_content);//"Android.intent.action.GET_CONTENT"Innerintent.settype (contentType);//view type String image_unspecified = "image/*";Intent wrapperintent= Intent.createchooser (Innerintent,NULL);   (Activity) context). Startactivityforresult (Wrapperintent, Requestcode); //VideoIntent innerintent=NewIntent (intent.action_get_content); Innerintent.settype (ContentType); //String video_unspecified = "video/*";Intent wrapperintent= Intent.createchooser (Innerintent,NULL);   (Activity) context). Startactivityforresult (Wrapperintent, Requestcode); //Add AudioIntent innerintent=NewIntent (intent.action_get_content); Innerintent.settype (ContentType); //String video_unspecified = "video/*";Intent wrapperintent= Intent.createchooser (Innerintent,NULL);   (Activity) context). Startactivityforresult (Wrapperintent, Requestcode); //recordingIntent Intent=NewIntent (intent.action_get_content); Intent.settype (CONTENTTYPE.AUDIO_AMR); //String audio_amr = "Audio/amr";Intent.setclassname ("Com.android.soundrecorder","Com.android.soundrecorder.SoundRecorder");   (Activity) context). Startactivityforresult (Intent, Requestcode); //shooting Video    intDurationlimit = Getvideocapturedurationlimit ();//systemproperties.getint ("ro.media.enc.lprof.duration");Intent Intent=NewIntent (mediastore.action_video_capture); Intent.putextra (mediastore.extra_video_quality,0);  Intent.putextra (Mediastore.extra_size_limit, sizeLimit);  Intent.putextra (Mediastore.extra_duration_limit, durationlimit);   Startactivityforresult (Intent, request_code_take_video); //photo Request_code_take_picture for the returned identityIntent Intent=NewIntent (mediastore.action_image_capture);//"Android.media.action.IMAGE_CAPTURE";Intent.putextra (Mediastore.extra_output, Mms.ScrapSpace.CONTENT_URI);//Output,uri.parse ("Content://mms/scrapspace ");Startactivityforresult (Intent, request_code_take_picture);

Android footstep---itent.action_pick, startactivityforresult

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.