我的Activity上有三个imageView。在每个ImageView中,我想上传另一张图片。如果用户单击图标(image1 / image2 / image3),则会打开galery,以便用户可以选择图片将其上载到imageView上。在我的代码中,我只有一个imageView工作。
所以我的问题。如何修改onActivityResult方法以检查哪个imageView是clickt,并在其上调用setImageBitmapMethod?
这是我的代码
image1 = (ImageView)findViewById(R.id.image_three_1);
image2 = (ImageView)findViewById(R.id.image_three_2);
image3 = (ImageView)findViewById(R.id.image_three_3);
image1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}
});
image2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}
});
image3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
image1.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
答案 0 :(得分:0)
感谢小费tuteja。我制作了三个不同的结果变量(RESULT_LOAD_IMAGE1 / RESULT_LOAD_IMAGE2 / RESULT_LOAD_IMAGE3)然后我在右边的RESULT_LOAD_IMAGE1变量上设置了右图像。
这里有适合我的代码
private static final int RESULT_LOAD_IMAGE1 = 1;
private static final int RESULT_LOAD_IMAGE2 = 2;
private static final int RESULT_LOAD_IMAGE3 = 3;
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout_for_three);
//load titles from string.xml
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
//load icons from string.xml
navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
set(navMenuTitles, navMenuIcons);
image1 = (ImageView)findViewById(R.id.image_three_1);
image2 = (ImageView)findViewById(R.id.image_three_2);
image3 = (ImageView)findViewById(R.id.image_three_3);
image1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE1);
}
});
image2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE2);
}
});
image3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE3);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE1 && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
image1.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
if (requestCode == RESULT_LOAD_IMAGE2 && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
image2.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
if (requestCode == RESULT_LOAD_IMAGE3 && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
image3.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}