简体   繁体   English

在Android应用程序中从图库中选取图像

[英]image picking from gallery in android application

I have the following code which allows me to open the gallery on my mobile device when the button is clicked. 我有以下代码,单击该按钮后,我可以在移动设备上打开图库。 When I select an image it then displays this chosen image in my application. 当我选择图像时,它将在我的应用程序中显示该选择的图像。 However, I need to be able to select two different images which I will then use to compare the structure of both. 但是,我需要能够选择两个不同的图像,然后将它们用于比较两者的结构。 I have tried a number of ways of doing this but have been unable to get anything working so far. 我已经尝试了多种方法来执行此操作,但到目前为止仍无法正常工作。

I want to be able to select two different images and then allow the user to either change these if they have selected an incorrect image or to move forward and start with the main purpose of the app, comparing structural similarities. 我希望能够选择两个不同的图像,然后允许用户更改它们(如果他们选择的图像不正确),或者继续前进并从应用程序的主要用途入手,比较结构相似性。

public class SelectImage extends Activity {

private static final int SELECT_PICTURE = 1;
private static int RESULT_LOAD_IMG = 1;

String selectedImagePath, selectedImagePath2;
ImageView img;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.select_image);

    img = (ImageView)findViewById(R.id.ImageView01);

    ((Button) findViewById(R.id.Button01)).setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
        Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                // Start the Intent
                startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
    }
    });
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            System.out.println("Image Path : " + selectedImagePath);
            img.setImageURI(selectedImageUri);
        }
    }
}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();        
    return cursor.getString(column_index);
}

} }

You won't be able to do it with the ACTION_PICK intent option. 使用ACTION_PICK intent选项将无法执行此操作。 To implement this, you'll need to use a custom ListView with the images. 要实现此目的,您需要对图像使用自定义ListView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM