📌  相关文章
📜  如何从 Android 中的图库中选择图像?

📅  最后修改于: 2022-05-13 01:55:38.033000             🧑  作者: Mango

如何从 Android 中的图库中选择图像?

当用户必须上传或将他们的图像设置为个人资料图片或用户想要向另一个人发送图片时,需要从 Android 中的图库中选择一个图像。因此,在本文中,逐步讨论了如何从图库中选择图像并预览所选图像。请看下图,本文进一步讨论了哪些内容。

从 Android 中的图库中选择图像

想要一个更快节奏和更具竞争力的环境来学习 Android 的基础知识吗?
单击此处前往由我们的专家精心策划的指南,旨在让您立即做好行业准备!

从图库中实现图像选择的步骤

第 1 步:创建一个空的活动项目

  • 创建一个空的活动 Android Studio 项目。并选择Java作为编程语言。
  • 参考安卓 |如何在 Android Studio 中创建/启动新项目?了解如何创建一个空的活动 Android Studio 项目。

第 2 步:使用 activity_main.xml

  • 该应用程序的主要布局包括一个用于打开图像选择器的按钮,以及一个用于从图库中预览所选图像的图像视图。
  • 要实现应用程序的布局,请调用activity_main.xml文件中的以下代码。
XML


  
    
    


Java
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
  
public class MainActivity extends AppCompatActivity {
  
    // One Button
    Button BSelectImage;
  
    // One Preview Image
    ImageView IVPreviewImage;
  
    // constant to compare 
      // the activity result code
    int SELECT_PICTURE = 200;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // register the UI widgets with their appropriate IDs
        BSelectImage = findViewById(R.id.BSelectImage);
        IVPreviewImage = findViewById(R.id.IVPreviewImage);
  
        // handle the Choose Image button to trigger 
          // the image chooser function
        BSelectImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageChooser();
            }
        });
    }
  
    // this function is triggered when
      // the Select Image Button is clicked
    void imageChooser() {
  
        // create an instance of the 
          // intent of the type image
        Intent i = new Intent();
        i.setType("image/*");
        i.setAction(Intent.ACTION_GET_CONTENT);
  
        // pass the constant to compare it 
          // with the returned requestCode
        startActivityForResult(Intent.createChooser(i, "Select Picture"), SELECT_PICTURE);
    }
  
    // this function is triggered when user
      // selects the image from the imageChooser
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
  
        if (resultCode == RESULT_OK) {
  
            // compare the resultCode with the
              // SELECT_PICTURE constant
            if (requestCode == SELECT_PICTURE) {
                // Get the url of the image from data
                Uri selectedImageUri = data.getData();
                if (null != selectedImageUri) {
                    // update the preview image in the layout
                    IVPreviewImage.setImageURI(selectedImageUri);
                }
            }
        }
    }
}


输出界面:

第 3 步:使用 MainActivity。 Java文件

  • 在这种情况下,imageChooser 被触发,意图是“图像”类型,动作为ACTION_GET_CONTENT。
  • 调用以下代码来实现相同的功能。添加注释以便更好地理解。

Java

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
  
public class MainActivity extends AppCompatActivity {
  
    // One Button
    Button BSelectImage;
  
    // One Preview Image
    ImageView IVPreviewImage;
  
    // constant to compare 
      // the activity result code
    int SELECT_PICTURE = 200;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // register the UI widgets with their appropriate IDs
        BSelectImage = findViewById(R.id.BSelectImage);
        IVPreviewImage = findViewById(R.id.IVPreviewImage);
  
        // handle the Choose Image button to trigger 
          // the image chooser function
        BSelectImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageChooser();
            }
        });
    }
  
    // this function is triggered when
      // the Select Image Button is clicked
    void imageChooser() {
  
        // create an instance of the 
          // intent of the type image
        Intent i = new Intent();
        i.setType("image/*");
        i.setAction(Intent.ACTION_GET_CONTENT);
  
        // pass the constant to compare it 
          // with the returned requestCode
        startActivityForResult(Intent.createChooser(i, "Select Picture"), SELECT_PICTURE);
    }
  
    // this function is triggered when user
      // selects the image from the imageChooser
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
  
        if (resultCode == RESULT_OK) {
  
            // compare the resultCode with the
              // SELECT_PICTURE constant
            if (requestCode == SELECT_PICTURE) {
                // Get the url of the image from data
                Uri selectedImageUri = data.getData();
                if (null != selectedImageUri) {
                    // update the preview image in the layout
                    IVPreviewImage.setImageURI(selectedImageUri);
                }
            }
        }
    }
}

输出:在模拟器上运行