will cede control to another application in order to accomplish this task.
In this case,we need another application to do something for us and we’re willing to allow the other application to take control of the user interface to do this.
create GoodShares app
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="example.goodshares" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".ShareActivity" android:label="@string/title_activity_share" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/button0" android:id="@+id/btn0" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/pic0" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/button1" android:id="@+id/btn1" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/pic1" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/text0" /> <Button android:id="@+id/next" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/next" /> </LinearLayout> </ScrollView>
public class ShareActivity extends Activity { Uri photoUri0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.share); Button button=(Button)findViewById(R.id.btn0); button.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub //ACTION_GET_CONTENT:a standard intent Intent request=new Intent(Intent.ACTION_GET_CONTENT); request.setType("image/*");//cause the intent to be routed to the Gallery //launch separate app,give up control flow startActivityForResult(request,0); } }); Button button1=(Button)findViewById(R.id.btn1); button1.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent request=new Intent("example.mash.Action"); request.addCategory(Intent.CATEGORY_DEFAULT); request.putExtra("example.mash.EXTRA_PHOTO", photoUri0); //launch separate app,give up control flow startActivityForResult(request,1); } }); } protected void onActivityResult(int requestCode, int resultCode, Intent data){ if(requestCode==0){ //retrieve data sent by Gallery photoUri0=(Uri) data.getParcelableExtra(Intent.EXTRA_STREAM); if(photoUri0==null&&data.getData()!=null){ photoUri0=data.getData(); } ImageView imgView0=(ImageView)findViewById(R.id.pic0); imgView0.setImageURI(photoUri0); }else if(requestCode==1){ //retrieve data sent by Mash app Uri photoUri1=(Uri)data.getParcelableExtra("example.mash.EXTRA_RESULT"); if(photoUri1==null&&data.getData()!=null){ photoUri1=data.getData(); } ImageView imgView1=(ImageView)findViewById(R.id.pic1); imgView1.setImageURI(photoUri1); } } }
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="example.mash" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MashActivity" android:label="@string/title_activity_image_mash" > <intent-filter> <action android:name="example.mash.ACTION" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application> </manifest>must use the example.mash.ACTION action in an Intent
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/image"/> <Button android:id="@+id/button" android:text="mash"/> </RelativeLayout>create MashActivity class
public class MashActivity extends Activity { public static final String EXTRA_PHOTO ="example.mash.EXTRA_PHOTO"; private static final int RESULT_ERROR = 99; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Intent request=getIntent(); if(request!=null &&request.hasExtra(EXTRA_PHOTO)){ //get uri of inbound image final Uri uri=(Uri)request.getParcelableExtra(EXTRA_PHOTO); ImageView image=(ImageView)findViewById(R.id.image); image.setImageURI(uri); Button button=(Button)findViewById(R.id.button); button.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub try{ //load image into memory Bitmap bmp=BitmapFactory.decodeStream(getContentResolver().openInputStream(uri)); Bitmap mashed=mash(bmp); Uri resultUri=saveImage(mashed); Intent response=new Intent(); response.putExtra("example.mash.EXTRA_RESULT", resultUri); //set result,pass intent back MashActivity.this.setResult(Activity.RESULT_OK,response); }catch(FileNotFoundException e){ Log.e("MashActivity", "Exception mashing pic", e); MashActivity.this.setResult(RESULT_ERROR); } finish();//release control } }); } } protected Bitmap mash(Bitmap bmap){ //处理操作省略... return bmap; } protected Uri saveImage(Bitmap bmap){ //处理操作省略 return null; } }