簡體   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