SoFunction
Updated on 2025-04-11

Detailed explanation of the examples of selecting images from the gallery in Android

android select pictures from the gallery

In android, how to select pictures from gallary gallary? It is actually very simple. The steps are as follows

1) Design an imageview to display the pictures selected in the gallery

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:andro 
  android:orientation="vertical" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  > 
  <ImageView 
      android: 
      android:layout_width="fill_parent" 
      android:layout_weight="1" android:layout_height="wrap_content"></ImageView> 
  <Button  
      android:layout_height="wrap_content"  
      android:text="Load Picture"  
      android:layout_width="wrap_content"  
      android:  
      android:layout_weight="0"  
      android:layout_gravity="center"></Button> 
</LinearLayout> 



2) Learn how to call up gallary in the key, which is actually intent, as follows

  Intent i = new Intent(Intent.ACTION_PICK, android.
.EXTERNAL_CONTENT_URI);
 startActivityForResult(i, RESULT_LOAD_IMAGE); 

3) Then after calling up the image gallery in onActivityResult, we will redisplay the image in the imageview of the page, so the code is as follows:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
  (requestCode, resultCode, data); 
   
  if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { 
    Uri selectedImage = (); 
    String[] filePathColumn = {  }; 
 
    Cursor cursor = getContentResolver().query(selectedImage, 
        filePathColumn, null, null, null); 
    (); 
 
    int columnIndex = (filePathColumn[0]); 
    String picturePath = (columnIndex); 
    (); 
     
    ImageView imageView = (ImageView) findViewById(); 
    ((picturePath)); 
   
  } 

Among them isUri selectedImage = ();All the data in the picture in the gallery has been obtained.

In this way, when the user selects the image in the gallery, it can be presented in the imageview control.

Thank you for reading, I hope it can help you. Thank you for your support for this site!