Android development-selecting images from the album without cropping

Source: Internet
Author: User

Android development-selecting images from the album without cropping
 

Problem:
In the first line of Guo Shen's code, select the image from the album in Chapter 8th, and select a cropped album to display it to the screen. However, after the image is selected from the album, the cropping interface is not displayed and is returned directly.

Solution:

When finding the source cause, we can find that output_image.jpg under the path of the SDK is a 0-byte file. So
This image is not generated. Then I thought there was a problem with the intention to send the photo selection to the system. I curiously checked the list file intent filter of the system's gallery application (gallery) source code, and found that:


                                      
                     
                                    
   
  

The android. intent. action. PICK action is used here. You can select an image or video. The book is android. intent. action. GET_CONTENT.
After modification, the cropping page is displayed.
In addition, I used
Uri selectedImage = data. getData (); // retrieve the Uri of the image returned by the System
The output shows that this is not an absolute path. This is incorrect when we set it to ImageVew or upload it to the network.

Demo:
Here is a demo of selecting images from an album without cropping them. It shows you how to obtain the absolute path and run it perfectly.
Create a new project, edit the activity_main.xml file, and add a button in the layout to select a photo and an imageview from the album. The Code is as follows:

    
  
   
Data-snippet-id = ext.2f9e1a0d919a8d10c752c253a8a5827c data-snippet-saved = false data-codota-status = done>
   
        
    
      
   

Then, modify the code in MainActivity and add the logic for selecting photos from the album. The Code is as follows:

Package com. example. selectfromgallery; import android. app. activity; 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. provider. mediaStore; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. imageView; public class MainActivity extends Activity {private Button chooseFromAlbum; private ImageView picImageView; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); picImageView = (ImageView) findViewById (R. id. view); chooseFromAlbum = (Button) findViewById (R. id. choose_from_album); chooseFromAlbum. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub selectPicture ();}});} /*** select a photo from the album (no cropping) */private void selectPicture () {// TODO Auto-generated method stub Intent intent = new Intent (); intent. setAction (Intent. ACTION_PICK); // Pick an item from the data intent. setType (image/*); // select startActivityForResult (intent, 1) from all images;} @ Override public void onActivityResult (int requestCode, int resultCode, Intent data) {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); picImageView. setImageBitmap (bitmap);} catch (Exception e) {// TODO Auto-generatedcatch block e. printStackTrace () ;}} super. onActivityResult (requestCode, resultCode, data) ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}@ Override public boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest. xml. int id = item. getItemId (); if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item );}}

Resolution:
From MainActivity, you can find that selecting an image from the album is only two steps:
Step 1: Send the intention to select a photo to the system.

/*** Select a photo from the album (no cropping) */private void selectPicture () {// TODO Auto-generated method stub Intent intent = new Intent (); intent. setAction (Intent. ACTION_PICK); // Pick an item from the data intent. setType (image/*); // select startActivityForResult (intent, 1) from all images );}

Step 2: process the results returned by the onActivityResult system.

Public void onActivityResult (int requestCode, int resultCode, Intent data) {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); picImageView. setImageBitmap (bitmap);} catch (Exception e) {// TODO Auto-generatedcatch block e. printStackTrace () ;}} super. onActivityResult (requestCode, resultCode, data );}

The system returns the Uri of the selected photo, Uri selectedImage = data. getData (); then define String [] filePathColumn = {MediaStore. images. media. DATA}; find the specified Uri path in filePathColumn. Use cursor. getString (columnIndex); to obtain the absolute path of the photo.

 

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.