In Android, you can simply use intent to obtain the functions provided by installed applications. It is one of the key components of Android and has two main functions: one is to trigger the functions provided by other applications, and the other is to implement switching between activities in a single application.
Software developers use intent filter to declare that an application provides a specific function. This statement is in AndroidManifest. for example, the built-in Camera application has made the following declaration under the "Camera" tag in its manifest file:
[Html]
<Intent-filter>
<Action android: name = "android. media. action. IMAGE_CAPTURE"/>
<Action android: name = "android. intent. category. DEFAULT"/>
</Intent-filter>
To use the Camera application using intent, you only need to create an Intent to capture the filter declared above. The Code is as follows:
[Java]
Intent it = new Intent ("android. media. action. IMAGE_CAPTURE ");
But the above Code is obviously hard-coded, and the string "android. media. action. IMAGE_CAPTURE "if it changes in the future, our code also needs to be modified, which is not conducive to maintenance. Fortunately, the MediaStore class provides the constant ACTION_IMAGE_CAPTURE for developers to use. In this way, the string name change will be solved by themselves within Android, the external interface ACTION_IMAGE_CAPTURE remains unchanged. The improved code is as follows:
[Java]
Intent it = new Intent (android. provider. MediaStore. ACTION_IMAGE_CAPTURE );
StartActivity (it );
1) return data from the Camera Application
It makes no sense to only capture images without storing them or other processing. To obtain the images captured by the Camera application, we only need to use the startActivityForResult function instead of startActivity, at the same time, you can reload the onActivityResult function of the Activity. The data returned from Camera is processed as Bitmap. The Code is as follows:
[Java]
<Span style = "font-size: 18px;"> package hust. iprai. asce1885.promedia;
Import android. app. Activity;
Import android. content. Intent;
Import android. graphics. Bitmap;
Import android. OS. Bundle;
Import android. widget. ImageView;
Public class ImageCaptureActivity extends Activity {
Final static int CAMERA_RESULT = 0;
ImageView iv = null;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Intent it = new Intent (android. provider. MediaStore. ACTION_IMAGE_CAPTURE );
StartActivityForResult (it, CAMERA_RESULT );
}
@ Override
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
Super. onActivityResult (requestCode, resultCode, data );
If (RESULT_ OK = resultCode ){
// Get Extra from the intent
Bundle extras = data. getExtras ();
// Get the returned image from extra
Bitmap bmp = (Bitmap) extras. get ("data ");
Iv = (ImageView) findViewById (R. id. ReturnedImageView );
Iv. setImageBitmap (bmp );
}
}
} </Span>
The corresponding layout/main. xml file is as follows:
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<ImageView android: id = "@ + id/ReturnedImageView"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
</LinearLayout>
Compile and run the above Code. We found that the captured image is very small, because when the Camera application is triggered by intent, it does not return a full-size image for the Activity that calls it, in this way, the memory of the mobile device is limited, and the memory occupied by the complete image is not small.
2) Save the captured image
If you want to directly Save the image captured by the Camera as an image, you can pass an additional parameter when calling the Camera application and specify the URI for storing the image. The additional parameter name is the constant EXTRA_OUTPUT defined in MediaStore. The following code segment saves the image caught by camera to the SDK and is named myfavoritepicture.jpg:
[Java]
String imageFilePath =
Environment. getExternalStorageDirectory (). getAbsolutePath () +
"/Myfavoritepicture.jpg ";
File imageFile = new File (imageFilePath );
Uri imageFileUri = Uri. fromFile (imageFile );
Intent it = new Intent (android. provider. MediaStore. ACTION_IMAGE_CAPTURE );
It. putExtra (android. provider. MediaStore. EXTRA_OUTPUT, imageFileUri );
StartActivityForResult (it, CAMERA_RESULT );
3) display large images
Loading and displaying images usually occupy a large amount of memory space. To reduce the possibility of memory exhaustion, Android provides a tool class called BitmapFactory, it provides a series of static functions to load Bitmap images from different sources. First, let's look at the BitmapFactory. Options class, which allows us to define how bitmap is read into memory. For example, we can set the sample size used when BitmapFactory loads an image. options. the value of inSampleSize is enough. The following code snippet indicates that inSampleSize is set to 8, which makes the loaded image 1/8 of the original image size:
[Java]
BitmapFactory. Options BMP factoryoptions = new BitmapFactory. Options ();
BMP factoryoptions. inSampleSize = 8;
Bitmap bmp = BitmapFactory. decodeFile (imageFilePath, bmp factoryoptions );
This is a quick way to load large images, but it does not take into account the actual size of the image and the size of the mobile phone screen. In reality, after scaling the image, it is usually necessary to fit the screen size.
We generally calculate the value of inSampleSize Based on the screen size. The code for obtaining the screen width is as follows:
[Java]
Display currentDisplay = getWindowManager (). getdefadisplay Display ();
Int dw = currentDisplay. getWidth ();
Int dh = currentDisplay. getHeight ();
To obtain the actual image size, we still use BitmapFactory. options class, And BitmapFactory. options. the inJustDecodeBounds variable is set to true, which tells the BitmapFactory class to calculate only the image size without actually decoding the image:
[Java]
// Load up the image's dimensions not the image itself
BitmapFactory. Options BMP factoryoptions = new BitmapFactory. Options ();
BMP factoryoptions. inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory. decodeFile (imageFilePath, bmp factoryoptions );
Int heightRatio = (int) Math. ceil (BMP factoryoptions. outHeight/(float) dh );
Int widthRatio = (int) Math. ceil (BMP factoryoptions. outWidth/(float) dw );
If the ratio of height to width is greater than 1, we take a larger ratio as the value of inSampleSize when narrowing down the image:
[Java]
// If both of the ratios are greater than 1,
// One of the sides of the image is greater than the screen
If (heightRatio> 1) & (widthRatio> 1 )){
If (heightRatio> widthRatio ){
// Height ratio is larger, scale according to it
BMP factoryoptions. inSampleSize = heightRatio;
} Else {
// Width ratio is larger, scale according to it
BMP factoryoptions. inSampleSize = widthRatio;
}
}
// Decode it for real
BMP factoryoptions. inJustDecodeBounds = false;
Bmp = BitmapFactory. decodeFile (imageFilePath, bmp factoryoptions );
The complete code for displaying large images is as follows. First look at the layout/main. xml file:
[Java]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<ImageView android: id = "@ + id/ReturnedImageView"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
</LinearLayout>
The following is the Java code section:
[Java]
Package hust. iprai. asce1885.promedia;
Import java. io. File;
Import android. app. Activity;
Import android. content. Intent;
Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. OS. Environment;
Import android. util. Log;
Import android. view. Display;
Import android. widget. ImageView;
Public class SizedCameraActivity extends Activity {
Final static int CAMERA_RESULT = 0;
ImageView iv = null;
String imageFilePath = "";
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
String imageFilePath = Environment. getExternalStorageDirectory (). getAbsolutePath () +
"/Myfavoritepicture.jpg ";
File imageFile = new File (imageFilePath );
Uri imageFileUri = Uri. fromFile (imageFile );
Intent it = new Intent (android. provider. MediaStore. ACTION_IMAGE_CAPTURE );
It. putExtra (android. provider. MediaStore. EXTRA_OUTPUT, imageFileUri );
StartActivityForResult (it, CAMERA_RESULT );
}
@ Override
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
Super. onActivityResult (requestCode, resultCode, data );
If (RESULT_ OK = resultCode ){
Iv = (ImageView) findViewById (R. id. ReturnedImageView );
Display currentDisplay = getWindowManager (). getdefadisplay Display ();
Int dw = currentDisplay. getWidth ();
Int dh = currentDisplay. getHeight ();
// Load up the image's dimensions not the image itself
BitmapFactory. Options BMP factoryoptions = new BitmapFactory. Options ();
BMP factoryoptions. inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory. decodeFile (imageFilePath, bmp factoryoptions );
Int heightRatio = (int) Math. ceil (BMP factoryoptions. outHeight/(float) dh );
Int widthRatio = (int) Math. ceil (BMP factoryoptions. outWidth/(float) dw );
Log. v ("HEIGHTRATIO", "" + heightRatio );
Log. v ("WIDTHRATIO", "" + widthRatio );
// If both of the ratios are greater than 1,
// One of the sides of the image is greater than the screen
If (heightRatio> 1) & (widthRatio> 1 )){
If (heightRatio> widthRatio ){
// Height ratio is larger, scale according to it
BMP factoryoptions. inSampleSize = heightRatio;
} Else {
// Width ratio is larger, scale according to it
BMP factoryoptions. inSampleSize = widthRatio;
}
}
// Decode it for real
BMP factoryoptions. inJustDecodeBounds = false;
Bmp = BitmapFactory. decodeFile (imageFilePath, bmp factoryoptions );
// Display it
Iv. setImageBitmap (bmp );
}
}
}
From ASCE1885's column