This article describes the definition and usage of Android programming picture operation classes. Share it for your reference, as follows:
Main interface category: taking photos and selecting album pictures
import ; import ; import ; import ; import ; import ; import ; import ; import ; /** * Picture operation in Android (photographs, photo album picture selection and picture cropping) * Author: ldm * Time: 20162016/7/11 09:09 */ public class ImageTestActivity extends Activity implements { //Photograph private Button take_photo; //Select pictures from the album private Button local_pic; //Picture display private ImageView upload_image; //Define operation constant private final static int TAKE_PHOTO_REQUEST = 1; private final static int LOCAL_PICS_REQUEST = 2; private final static int UPLOAD_PIC_REQUEST = 3; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_image_test); //Initialize the control and listen to events initViews(); } private void initViews() { this.upload_image = (ImageView) findViewById(.upload_image); this.take_photo = (Button) findViewById(.take_photo); this.local_pic = (Button) findViewById(.local_pics); this.take_photo.setOnClickListener(this); this.local_pic.setOnClickListener(this); } @Override public void onClick(View view) { if (() == .take_photo) {//Photograph //Calling the system to take pictures In Intent photoIn = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(photoIn, TAKE_PHOTO_REQUEST); } else if (() == .local_pics) {//Select from the album Intent picsIn = new Intent(Intent.ACTION_GET_CONTENT); ("image/*");//Set the selected data type as picture type startActivityForResult(picsIn, LOCAL_PICS_REQUEST); } } //After taking a photo or selecting an album, the data will be processed here @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { (requestCode, resultCode, data); if (null == data) { return; } switch (requestCode) { case TAKE_PHOTO_REQUEST: Bundle bundle = ();//Get picture data if (null != bundle) { Bitmap bm = ("data"); //Show the picture on ImView // upload_image.setImageBitmap(bm); //Cut the picture Uri uri = (bm); startImageCrop(uri); } break; case LOCAL_PICS_REQUEST: Uri uri = ();//The Uri from the picture starts with the format of content:// //Get the picture Bitmap bm = ImageUtils.uri2Bitmap(, uri); //Show the picture on ImView // upload_image.setImageBitmap(bm); //Save the photo taken to the local and convert it to file format Uri Uri fileUri = (bm); //Cut the picture startImageCrop(fileUri); break; case UPLOAD_PIC_REQUEST: //Show the cropped pictures Bundle b = (); Bitmap bitmap = ("data"); //The picture is displayed upload_image.setImageBitmap(bitmap); break; } } /** * @param * @description Image Cropping * @author ldm * @time 2016/7/11 10:07 */ private void startImageCrop(Uri uri) { Intent intent = new Intent(""); (uri, "image/*");//Set Uri and type ("crop", "true");// ("aspectX", 2);//The proportion in the X direction ("aspectY", 1);//The proportion in the Y direction ("outputX", 200);//The X-direction width of the cropping area ("outputY", 100);//The Y direction width of the cropping area ("scale", true);// Whether to retain proportion ("outputFormat", ()); ("return-data", true);// Whether to keep the data in Bitmap to return the corresponding Bitmap data of dataParcelable startActivityForResult(intent, UPLOAD_PIC_REQUEST); } }
Layout file
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android: android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:text="Photo upload" /> <Button android: android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:text="Local Upload" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Picture Information Display" android:layout_marginLeft="10dp" android:textSize="16sp"/> <ImageView android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical"/> </LinearLayout>
Image operation tool category
import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; /** * description: The Uri obtained from the picture starts with content://, and find the corresponding picture from U. * Author: ldm * Room: 20162016/7/11 09:47 */ public class ImageUtils { public static Bitmap uri2Bitmap(Context mContext, Uri uri) { InputStream in = null; try { in = ().openInputStream(uri); //Get picture from input stream Bitmap bm = (in); (); return bm; } catch (Exception e) { (); } return null; } /** * @param * @description Save the picture to the mobile phone SD card and return the file i corresponding to the picture * @author ldm * @time 2016/7/11 9:55 */ public static Uri saveBitmapToSdCard(Bitmap bm) { //Customize the image name String name = ("yyyyMMdd_hhmmss", ()) + ".png"; //Define the location of the image File tempFile = new File("/sdcard/Image/"); if (!()) { (); } String fileName = "/sdcard/Image/" + name; File pic = new File(fileName); try { FileOutputStream os = new FileOutputStream(pic); //Compress the picture (, 100, os); (); (); return (pic); } catch (Exception e) { (); } return null; } }
Finally, don't forget to add the corresponding permissions in:
<uses-permission android:name=".READ_EXTERNAL_STORAGE" /> <uses-permission android:name=".WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name=".MOUNT_UNMOUNT_FILESYSTEMS"/>
Note: For more instructions on Android permission control, please click here to viewAndroid permissions operation instructions
For more information about Android related content, please check out the topic of this site:Summary of Android graphics and image processing skills》、《Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.