This article shares the specific code of Android's function to select photos from albums for your reference. The specific content is as follows
Before"Android Development Cropped Photos"The article introduces how to select photos from the album and take photos and crop them. In this article, it mainly introduces selecting native photos from the album (not cropped).
first step:Send the system the intention to select a photo.
/** * Select native photos from the album (not cropped) */ private void selectFromGallery() { // TODO Auto-generatedmethod stub Intentintent=new Intent(); (Intent.ACTION_PICK);//Pick an item fromthe data ("image/*");//Select from all pictures startActivityForResult(intent, SELECT_ORIGINAL_PIC); }
Step 2:Process the results returned by the system.
switch (requestCode) { case SELECT_ORIGINAL_PIC: if (resultCode==RESULT_OK) {//Select photos from album without cutting try { Uri selectedImage = (); //Get the Uri of the photo returned by the system String[] filePathColumn = { }; Cursor cursor =getContentResolver().query(selectedImage, filePathColumn, null, null, null);//Query the photos corresponding to the specified Uri from the system table (); int columnIndex = (filePathColumn[0]); String picturePath = (columnIndex); //Get photo path (); Bitmap bitmap= (picturePath); (bitmap); } catch (Exception e) { // TODO Auto-generatedcatch block (); } } break; }
Code description:
After sending the intention to select a photo to the system, the system or starts the album management program to let the user select the photo. After selecting the photo, the system will return a Uri for selecting the photo. In order to obtain the absolute path of the photo corresponding to Uri, we need to look for the image path corresponding to the specified Uri in the system's media data frame. After obtaining the absolute path to the image, we can do some operations, such as setting it to ImageVew, uploading it to the network, etc.
Finally, the complete project code is attached:
package ; import ; import ; import .; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; /** * Select photos from the album to crop, take photos from the camera to crop<br> * Select a photo from the album (not cropped) and get the path to the photo<br> * Take photos (not cropped) and get the photo path * @author JPH * Date:2014.10.09 * last modified:2014.11.04 */ public class MainActivity extends ActionBarActivity { /**request Code Select photos from album and crop **/ private final static int SELECT_PIC=123; /**request Code Select photos from album without cropping**/ private final static int SELECT_ORIGINAL_PIC=126; /**request Code Take photos and crop them**/ private final static int TAKE_PIC=124; /**request Code Take photos without cropping**/ private final static int TAKE_ORIGINAL_PIC=127; /**request Code crop photo**/ private final static int CROP_PIC=125; private Uri imageUri; private ImageView imgShow; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); //Initialize imageUri imageUri=(new File((), "")); imgShow=(ImageView)findViewById(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub switch (requestCode) { case SELECT_PIC: if (resultCode==RESULT_OK) {//Select photos from the album and crop them try { Bitmap bitmap=(getContentResolver().openInputStream(imageUri));//Load the image of the imageUri object into memory (bitmap); } catch (Exception e) { // TODO Auto-generated catch block (); } } break; case SELECT_ORIGINAL_PIC: if (resultCode==RESULT_OK) {//Select photos from album without cutting try { Uri selectedImage = (); //Uri to obtain the photos returned by the system String[] filePathColumn = { }; Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);//Query the photos corresponding to the specified Uri from the system table (); int columnIndex = (filePathColumn[0]); String picturePath = (columnIndex); //Get photo path (); Bitmap bitmap= (picturePath); (bitmap); } catch (Exception e) { // TODO Auto-generated catch block (); } } break; case TAKE_PIC://Pick the photos and cut them if (resultCode==RESULT_OK) { cropImageUri(imageUri, 600, 600, CROP_PIC); } case TAKE_ORIGINAL_PIC://Pick a photo if (resultCode==RESULT_OK) { String imgPath=();//Get the path to take photos } break; case CROP_PIC://Pick a photo if (resultCode==RESULT_OK) { try { Bitmap bitmap=(getContentResolver(). openInputStream(imageUri));//Load the image of the imageUri object into memory (bitmap); } catch (FileNotFoundException e) { // TODO Auto-generated catch block (); } } break; default: break; } (requestCode, resultCode, data); } /** * Crop the photos corresponding to the specified uri * @param imageUri: the corresponding photo of 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(""); (imageUri, "image/*"); ("crop", "true"); ("aspectX", 1); ("aspectY", 1); ("outputX", outputX); ("outputY", outputY); ("scale", true); (MediaStore.EXTRA_OUTPUT, imageUri); ("return-data", false); ("outputFormat", ()); ("noFaceDetection", true); // no face detection startActivityForResult(intent, requestCode); } public void cropPic(View view) { switch (()) { case ://select photos from the album for cropping cropFromGallery(); break; case ://take photos from the camera for cropping cropFromTake(); break; case ://select photos from album without cutting selectFromGallery(); break; case ://take photos from the camera without cropping selectFromTake(); break; default: break; } } /** * Select native photos from the album (not cropped) */ private void selectFromGallery() { // TODO Auto-generated method stub Intent intent=new Intent(); (Intent.ACTION_PICK);//Pick an item from the data ("image/*");//Select from all pictures startActivityForResult(intent, SELECT_ORIGINAL_PIC); } /** * Select photos from the album to crop */ private void cropFromGallery() { // TODO Auto-generated method stub Intent intent=new Intent(); (Intent.ACTION_PICK);//Pick an item from the data ("image/*");//Select from all pictures ("crop", "true");//Set to crop ("aspectX", 1);//Wide ratio of crop ("aspectY", 1);//High proportion of cropping ("outputX", 600);//Crop width ("outputY", 600);//Crop height ("scale", true);//Scaling is supported ("return-data", false); (MediaStore.EXTRA_OUTPUT, imageUri);//Output the cropped result to the specified Uri ("outputFormat", ());//The format of the cut image ("noFaceDetection", true); // no face detection startActivityForResult(intent, SELECT_PIC); } /** * Take photos without cutting */ private void selectFromTake() { // TODO Auto-generated method stub Intent intent=new Intent(); (MediaStore.ACTION_IMAGE_CAPTURE);//Set Action as photoshoot (MediaStore.EXTRA_OUTPUT, imageUri);//Save the captured photos to the specified URI startActivityForResult(intent, TAKE_ORIGINAL_PIC); } /** * Take photos from the camera for cropping */ private void cropFromTake() { // TODO Auto-generated method stub Intent intent=new Intent(); (MediaStore.ACTION_IMAGE_CAPTURE);//Set Action as photoshoot (MediaStore.EXTRA_OUTPUT, imageUri);//Save the captured photos to the specified URI startActivityForResult(intent, TAKE_PIC); } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.