Start and close a new activity and obtain the data of the original activity.

Source: Internet
Author: User

I wrote a similar tool based on the image cropping function of the Android system. The function is basically implemented, but there is a call question: how can I call this cropping tool from my program and get the cropped image?

In fact, this is also very simple, that is, the basic usage of intent.

First, go to the previous figure (the interface is still not optimized. It will be ugly if it is ugly ):

Start activity. Open the Image Selection window and select an image.

I wiped it. This figure is really large ..

The following is the jump to the cropping page.

Press the crop button to exit the activity, return to the original interface, and display the cropped chart

 

 

 

This process simulates the overall process of the system cropping function. The following is the key code and description for implementing the function.

Here, the main program is called a, and the called subroutine is called B.

B is a self-written program. When calling B, you need to customize your own activity action. The system action corresponds to an action string, such as some action constants defined in the intent class:

public static final String ACTION_MAIN = "android.intent.action.MAIN";public static final String ACTION_VIEW = "android.intent.action.VIEW";public static final String ACTION_EDIT = "android.intent.action.EDIT";public static final String ACTION_CALL = "android.intent.action.CALL";public static final String ACTION_CALL_EMERGENCY = "android.intent.action.CALL_EMERGENCY";

The intent constructor contains some methods such as intent. setclass (activity. This, X. Class. But I do not like this method.
In addition, the common constructor is

public Intent(String action);public Intent(String action, Uri uri);

Using string, the intent object of the URI parameter type is called the implicit intent object, that is, the intent class constructor does not specify the activity of the intent, and these goals depend on androidmanifest. the configuration information in the XML file can be determined. In other words, an action may refer to more than one target, or multiple activity actions that receive the same action can be configured in the androidmanifest. xml file.

In androidmanifest. XML, the file format is as follows:

 1     <application 2         android:allowBackup="true" 3         android:icon="@drawable/ic_launcher" 4         android:label="@string/app_name" 5         android:theme="@style/AppTheme" > 6         <activity 7             android:name="com.example.crop_image_my.MainActivity" 8             android:label="@string/app_name" > 9             <intent-filter>10                 <action android:name="android.intent.action.MAIN" />11 12                 <category android:name="android.intent.category.LAUNCHER" />13             </intent-filter>14         </activity>15     </application>

The <action> tag specifies a system-defined activity action. This action indicates that the first activity started when the application starts needs to receive this action. That is to say, this action is used by the Android app to start the main window.

Because you need to start the cropping program with your own action, I added the following section in the XML above:

1 <intent-filter>2   <action android:name="com.example.crop_image_my.copper" />3 4     <data android:scheme="crop" />5 6     <category android:name="android.intent.category.DEFAULT" />7 </intent-filter>

Keep. Main: I need to run the program separately. (I don't know what will happen if I delete it. I didn't test it)

The <DATA> label above defines a scheme, so the URI below can write crop: // something

Oh, yes. The above is the configuration file of B. A wants to start B.

The intent of B can be called in a as follows:

1     private void startMyCropIntent(String path) throws FileNotFoundException {2         Intent intent = new Intent("com.example.crop_image_my.copper", Uri.parse("crop://" + path));3         startActivityForResult(intent, 12);4     }

The above intent structure will not be mentioned, and the parameters are in the previous configuration file. To start a new activity, you can use startactivity (). However, to obtain the returned value, you must use the startactivityforresult () method to process it in onactivityresult.

The second parameter 12 is the request code, which corresponds to the first parameter in onactivityresult (INT requestcode, int resultcode, intent data. This number can be written at will, but it is recommended to use resources. For example, if a button triggers startactivityforresult (), you can set the r of this button. id. button1 is used as the Request Code (in fact, everything can be used, as long as it is convenient to identify ).

After the activity is started, note that the parameter has a URI. This URI is the path of the image I selected. You need to obtain this URI in Method B of cropping:

1 If (getintent (). getdata ()! = NULL) {2 imgpath = getintent (). getdata (). getpath (); // see the URI composition document 3 log. V ("<dbw>", imgpath); 4 mycropview. setbmp path (imgpath); 5}

Written in oncreate, this value is obtained and used during construction.

The following describes how to disable an activity. Write in B. Put the Code directly.

 1     @Override 2     public void onClick(View v) { 3         // TODO Auto-generated method stub 4         switch (v.getId()) { 5         case R.id.btn_crop: 6             Bitmap croppedImage = myCropView.getCroppedImage(); 7  8             croppedImageView.setImageBitmap(croppedImage); 9             saveCroppedImage(croppedImage);10 11             // return to the last activity12             Log.v("<DBW>", newFilePath);13             getIntent().putExtra("newPath", newFilePath);14             setResult(20, getIntent());15             break;16         case R.id.btn_cancel:17             setResult(21);18             break;19         default:20             break;21         }22         finish();23     }

Save the image after cropping, and use the getintent (). putextra () method to store the image path in the intent. "Newpath" is a random name, which is used as a data identification. Finish () is to close this activity. Parameter 20 is the second parameter of onactivityresult (INT requestcode, int resultcode, intent data.

Finally, obtain the captured image and write it in:

1 @ override 2 protected void onactivityresult (INT requestcode, int resultcode, intent data) {3 super. onactivityresult (requestcode, resultcode, data); 4 5 switch (requestcode) {6 Case 12: 7 if (resultcode = 20) {8 string Path = data. getextras (). getstring ("newpath"); 9 log. V ("<dbw>", "Get ------" + path); 10 bitmap BMP = bitmapfactory. decodefile (PATH); 11 IV. setimagebitmap (BMP); 12} else if (resultcode = 21) {13 Toast = toast. maketext (this, "you canceled the operation", toast. length_long); 14 toast. show (); 15} 16 break; 17 default: 18 break; 19} 20 21}

The switch is for different activities (currently only one is started and the ID code is 12 ). Perform different processing on different resultcodes.

We used putextra to set data. Here we use the data. getextra method to get the bundle object and use the getxxx method to get different data as needed.

 

This is the process.

Start and close a new activity and obtain the data of the original activity.

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.