Android enables local album selection and returns to display,

Source: Internet
Author: User

Android enables local album selection and returns to display,

. Java

1 package com. jerry. crop; 2 3 import java. io. file; 4 5 import android. app. activity; 6 import android. content. intent; 7 import android. graphics. bitmap; 8 import android.net. uri; 9 import android. OS. bundle; 10 import android. OS. environment; 11 import android. provider. mediaStore; 12 import android. view. view; 13 import android. widget. imageView; 14 import android. widget. toast; 15 16 public class MainAct Ivity extends Activity {17 18 private static final int PHOTO_REQUEST_CAREMA = 1; // photograph 19 private static final int PHOTO_REQUEST_GALLERY = 2; // select 20 private static final int PHOTO_REQUEST_CUT = 3 from the album; // Result 21 22 private ImageView iv_image; 23 24/* avatar name */25 private static final String PHOTO_FILE_NAME = "temp_photo.jpg"; 26 private File tempFile; 27 28 @ Override 29 protected void onCreate (Bundle SavedInstanceState) {30 super. onCreate (savedInstanceState); 31 setContentView (R. layout. activity_main); 32 this. iv_image = (ImageView) this. findViewById (R. id. iv_image); 33} 34 35/* 36 * Get 37 */38 public void gallery (View view) {39 // activate the system image library, select an image 40 Intent intent = new Intent (Intent. ACTION_PICK); 41 intent. setType ("image/*"); 42 // enable an Activity with a returned value. The request code is PHOTO_REQUEST_GALLERY 43 startAct. IvityForResult (intent, PHOTO_REQUEST_GALLERY); 44} 45 46/* 47 * Get 48 */49 public void camera (View view) from the camera) {50 // activate camera 51 Intent intent = new Intent ("android. media. action. IMAGE_CAPTURE "); 52 // determines whether the memory card can be used. 53 if (hasSdcard () {54 tempFile = new File (Environment. getExternalStorageDirectory (), 55 PHOTO_FILE_NAME); 56 // create uri 57 Uri uri Uri = uri from the file. fromFile (tempFile); 58 intent. putExtra (Me DiaStore. EXTRA_OUTPUT, uri); 59} 60 // activate an Activity with a returned value. The request code is PHOTO_REQUEST_CAREMA 61 startActivityForResult (intent, PHOTO_REQUEST_CAREMA ); 62} 63 64/* 65 * Cut image 66 */67 private void crop (Uri uri) {68 // crop image Intent 69 intent Intent = new Intent ("com. android. camera. action. CROP "); 70 intent. setDataAndType (uri, "image/*"); 71 intent. putExtra ("crop", "true"); 72 // ratio of the cropping frame, intent. putExtra ("aspe CtX ", 1); 74 intent. putExtra ("aspectY", 1); 75 // the size of the output image after cropping is 76 intent. putExtra ("outputX", 250); 77 intent. putExtra ("outputY", 250); 78 79 intent. putExtra ("outputFormat", "JPEG"); // The image format is 80 intent. putExtra ("noFaceDetection", true); // cancel face recognition 81 intent. putExtra ("return-data", true); 82 // enable an Activity with a returned value. The request code is PHOTO_REQUEST_CUT 83 startActivityForResult (intent, PHOTO_REQUEST_CUT); 84} 85 86/* 8 7 * determine whether the sdcard is mounted 88 */89 private boolean hasSdcard () {90 if (Environment. getExternalStorageState (). equals (91 Environment. MEDIA_MOUNTED) {92 return true; 93} else {94 return false; 95} 96} 97 98 @ Override 99 protected void onActivityResult (int requestCode, int resultCode, Intent data) {100 if (requestCode = PHOTO_REQUEST_GALLERY) {101 // The data returned from the album is 102 if (data! = Null) {103 // obtain the full path of the image 104 Uri = data. getData (); 105 crop (uri); 106} 107 108} else if (requestCode = PHOTO_REQUEST_CAREMA) {109 // data returned from the camera 110 if (hasSdcard ()) {111 crop (Uri. fromFile (tempFile); 112} else {113 Toast. makeText (MainActivity. this, "no memory card found, unable to store photos! ", 0). show (); 114} 115 116} else if (requestCode = PHOTO_REQUEST_CUT) {117 // 118 if (data! = Null) {119 Bitmap bitmap = data. getParcelableExtra ("data"); 120 this. iv_image.setImageBitmap (bitmap); 121} 122 try {123 // Delete the temporary file 124 tempFile. delete (); 125} catch (Exception e) {126 e. printStackTrace (); 127} 128 129} 130 131 super. onActivityResult (requestCode, resultCode, data); 132} 133}

. Xml

1 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 2 xmlns: tools = "http://schemas.android.com/tools" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 android: orientation = "vertical" 6 tools: context = ". mainActivity "> 7 8 <Button 9 android: layout_width =" wrap_content "10 android: layout_height =" wrap_content "11 android: onClick =" gallery "12 android: text = "Get gallery image"/> 13 <Button14 android: layout_width = "wrap_content" 15 android: layout_height = "wrap_content" 16 android: onClick = "camera" 17 android: text = ""/> 18 19 <ImageView20 android: id = "@ + id/iv_image" 21 android: layout_width = "wrap_content" 22 android: layout_height = "wrap_content"/> 23 24 </LinearLayout>

 

:

 

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.