Problem with multiple image selection in android
I have a problem in selecting image fr开发者_高级运维om image gallery. In my view there are two Buttons and two EditTexts. On clicking each button I want to choose and image from gallery and the path of the image needs to copied into the respective EditText. In my code image selection is working but the path is entered into only one EditText for both buttons. Please help me.
My Code:
private String imageType;
public void browsePositionImage(View button)
{
imageType = "POSITION";
startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI), SELECT_IMAGE );
}
public void browseObjectImage(View button)
{
imageType = "OBJECT";
startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI), SELECT_IMAGE );
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_IMAGE)
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
if(imageType == "POSITION")
{
final EditText txtPImage = (EditText) findViewById(R.id.EditTextPositionImage);
txtPImage.setText(getRealPathFromURI(selectedImage));
}
else
{
final EditText txtPImage = (EditText) findViewById(R.id.EditTextObjectImage);
txtPImage.setText(getRealPathFromURI(selectedImage));
}
}
}
Try something like this
public static final int SELECT_POSITION_IMAGE = 1;
public static final int SELECT_OBJECT_IMAGE = 2;
public void browsePositionImage(View button)
{
startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI), SELECT_POSITION_IMAGE );
}
public void browseObjectImage(View button)
{
startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI), SELECT_OBJECT_IMAGE );
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if(requestCode == SELECT_POSITION_IMAGE)
{
Uri selectedImage = data.getData();
final EditText txtPImage = (EditText) findViewById(R.id.EditTextPositionImage);
txtPImage.setText(getRealPathFromURI(selectedImage));
}
else if (requestCode == SELECT_OBJECT_IMAGE)
{
Uri selectedImage = data.getData();
final EditText txtPImage = (EditText) findViewById(R.id.EditTextObjectImage);
txtPImage.setText(getRealPathFromURI(selectedImage));
}
}
}
精彩评论