繁体   English   中英

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

[英]image picking from gallery in android application

我有以下代码,单击该按钮后,我可以在移动设备上打开图库。 当我选择图像时,它将在我的应用程序中显示该选择的图像。 但是,我需要能够选择两个不同的图像,然后将它们用于比较两者的结构。 我已经尝试了多种方法来执行此操作,但到目前为止仍无法正常工作。

我希望能够选择两个不同的图像,然后允许用户更改它们(如果他们选择的图像不正确),或者继续前进并从应用程序的主要用途入手,比较结构相似性。

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);
}

}

使用ACTION_PICK intent选项将无法执行此操作。 要实现此目的,您需要对图像使用自定义ListView。

暂无
暂无

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

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