简体   繁体   中英

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. To implement this, you'll need to use a custom ListView with the images.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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