Select Photos from album in Android development,

Source: Internet
Author: User

Select Photos from album in Android development,

Select Photos from album in Android Development


Please respect the fruits of others' work, and repost them with the source:Select Photos from album in Android Development

I once introduced how to select, take, and crop photos from an album in "crop photos for Android development, in this article, we will introduce how to select native photos from the album without cropping them ).

Step 1: Send the intention to select a photo to the system.

/*** Select native photos from the album (not cropping) */private void selectFromGallery () {// TODO Auto-generatedmethod stub Intentintent = new Intent (); intent. setAction (Intent. ACTION_PICK); // Pick an item fromthe data intent. setType ("image/*"); // select startActivityForResult (intent, SELECT_ORIGINAL_PIC) from all images );}

Step 2: process the results returned by the system.

Switch (requestCode) {case SELECT_ORIGINAL_PIC: if (resultCode = RESULT_ OK) {// select a photo from the album without cropping try {Uri selectedImage = data. getData (); // retrieve the Uri String [] filePathColumn = {MediaStore. images. media. DATA}; Cursor cursor = getContentResolver (). query (selectedImage, filePathColumn, null); // query the photo cursor corresponding to the specified Uri from the system table. moveToFirst (); int columnIndex = cursor. getColumnIndex (filePathColumn [0]); String picturePath = cursor. getString (columnIndex); // obtain the photo path cursor. close (); Bitmap bitmap = BitmapFactory. decodeFile (picturePath); imgShow. setImageBitmap (bitmap);} catch (Exception e) {// TODO Auto-generatedcatch block e. printStackTrace () ;}} break ;}

Code Description:

After sending the intention to select a photo to the system, the system may start the album management program to allow users to select the photo. After selecting the photo, the system will return a Uri for the selected photo, to obtain the absolute path of the image corresponding to the Uri, We need to search for the image path corresponding to the specified Uri in the system's media data box. After obtaining the absolute path of the image, we can perform some operations, such as setting it to ImageVew and uploading it to the network.

Complete project code is attached:

Package com. jph. cp; import java. io. file; import java. io. fileNotFoundException; import android. support. v7.app. actionBarActivity; import android. content. intent; import android. database. cursor; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android.net. uri; import android. OS. bundle; import android. OS. environment; import android. provider. mediaStore; import android. view. view; import android. widget. imageView;/*** select a photo from the album for cropping, and take a photo from the camera for cropping <br> * select a photo from the album (no cropping ), and get the photo path <br> * take the photo (without cropping), and get the photo path * @ author JPH * Date: 2014.10.09 * last modified: 2014.11.04 */public class MainActivity extends ActionBarActivity {/** request Code select a photo from the album and crop it **/private final static int SELECT_PIC = 123; /** request Code select the photo from the album without cropping **/private final static int SELECT_ORIGINAL_PIC = 126; /** request Code to take a photo and crop it **/private final static int TAKE_PIC = 124; /** request Code to take a photo without cropping **/private final static int TAKE_ORIGINAL_PIC = 127;/** request Code cropping photo **/private final static int CROP_PIC = 125; private Uri imageUri; private ImageView imgShow; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // initialize imageUriimageUri = Uri. fromFile (new File (Environment. getExternalStorageDirectory (), "test.jpg"); imgShow = (ImageView) findViewById (R. id. imgShow) ;}@ Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubswitch (requestCode) {case SELECT_PIC: if (resultCode = RESULT_ OK) {// select a photo from the album and crop the try {Bitmap bitmap = BitmapFactory. decodeStream (getContentResolver (). openInputStream (imageUri); // loads the image of the imageUri object to the memory imgShow. setImageBitmap (bitmap);} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} break; case SELECT_ORIGINAL_PIC: if (resultCode = RESULT_ OK) {// select a photo from the album without cropping try {Uri selectedImage = data. getData (); // retrieve the Uri String [] filePathColumn = {MediaStore. images. media. DATA}; Cursor cursor = getContentResolver (). query (selectedImage, filePathColumn, null); // query the photo cursor corresponding to the specified Uri from the system table. moveToFirst (); int columnIndex = cursor. getColumnIndex (filePathColumn [0]); String picturePath = cursor. getString (columnIndex); // obtain the photo path cursor. close (); Bitmap bitmap = BitmapFactory. decodeFile (picturePath); imgShow. setImageBitmap (bitmap);} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} break; case TAKE_PIC: // take a photo and crop if (resultCode = RESULT_ OK) {cropImageUri (imageUri, 600,600, CROP_PIC);} case TAKE_ORIGINAL_PIC: // if (resultCode = RESULT_ OK) {String imgPath = imageUri. getPath (); // obtain the photo path} break; case CROP_PIC: // if (resultCode = RESULT_ OK) {try {Bitmap bitmap = BitmapFactory. decodeStream (getContentResolver (). openInputStream (imageUri); // loads the image of the imageUri object to the memory imgShow. setImageBitmap (bitmap);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} break; default: break;} super. onActivityResult (requestCode, resultCode, data);}/*** crop the photo corresponding to the specified uri * @ param imageUri: the photo corresponding to the uri * @ param outputX: crop width * @ param outputY: crop height * @ param requestCode: Request Code */private void cropImageUri (Uri imageUri, int outputX, int outputY, int requestCode) {Intent intent = new Intent ("com. android. camera. action. CROP "); intent. setDataAndType (imageUri, "image/*"); intent. putExtra ("crop", "true"); intent. putExtra ("aspectX", 1); intent. putExtra ("aspectY", 1); intent. putExtra ("outputX", outputX); intent. putExtra ("outputY", outputY); intent. putExtra ("scale", true); intent. putExtra (MediaStore. EXTRA_OUTPUT, imageUri); intent. putExtra ("return-data", false); intent. putExtra ("outputFormat", Bitmap. compressFormat. JPEG. toString (); intent. putExtra ("noFaceDetection", true); // no face detection startActivityForResult (intent, requestCode);} public void cropPic (View view) {switch (view. getId () {case R. id. btnCropFromGallery: // select a photo from the album to crop cropFromGallery (); break; case R. id. btnCropFromTake: // crop cropFromTake (); break; case R. id. btnOriginal: // select a photo from the album without cropping selectFromGallery (); break; case R. id. btnTakeOriginal: // do not crop selectFromTake (); break; default: break ;}/ *** Select native photos from the album (do not crop) */private void selectFromGallery () {// TODO Auto-generated method stubIntent intent = new Intent (); intent. setAction (Intent. ACTION_PICK); // Pick an item from the dataintent. setType ("image/*"); // select startActivityForResult (intent, SELECT_ORIGINAL_PIC) from all images );} /*** select a photo from the album for cropping */private void cropFromGallery () {// TODO Auto-generated method stubIntent intent = new Intent (); intent. setAction (Intent. ACTION_PICK); // Pick an item from the dataintent. setType ("image/*"); // select intent from all images. putExtra ("crop", "true"); // set it to crop intent. putExtra ("aspectX", 1); // intent for width ratio of cropping. putExtra ("aspectY", 1); // specifies a high proportion of intent cropping. putExtra ("outputX", 600); // specifies the width of the cropped intent. putExtra ("outputY", 600); // intent crop height. putExtra ("scale", true); // supports intent scaling. putExtra ("return-data", false); intent. putExtra (MediaStore. EXTRA_OUTPUT, imageUri); // output the cropping result to the specified Uriintent. putExtra ("outputFormat", Bitmap. compressFormat. JPEG. toString (); // The intent format of the cropped image. putExtra ("noFaceDetection", true); // no face detectionstartActivityForResult (intent, SELECT_PIC);}/*** take a photo without cropping */private void selectFromTake () {// TODO Auto-generated method stubIntent intent = new Intent (); intent. setAction (MediaStore. ACTION_IMAGE_CAPTURE); // sets the Action as the photo intent. putExtra (MediaStore. EXTRA_OUTPUT, imageUri); // Save the photo to the specified URI startActivityForResult (intent, TAKE_ORIGINAL_PIC );} /*** crop a photo taken from the camera */private void cropFromTake () {// TODO Auto-generated method stubIntent intent = new Intent (); intent. setAction (MediaStore. ACTION_IMAGE_CAPTURE); // sets the Action as the photo intent. putExtra (MediaStore. EXTRA_OUTPUT, imageUri); // Save the photo to the specified URI startActivityForResult (intent, TAKE_PIC );}}

If you think this blog post is helpful to you, please give it a compliment! You can also follow fengyuzhengfan's blog to learn more! Http://blog.csdn.net/fengyuzhengfan/





Related Article

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.