I have three ImageViews which I created by using a loop. I've already defined the variable pictureSelected in the Activity to track which ImageView is clicked. Strangely in onActivityResult, this pictureSelected variable value is reset to 0, regardless which ImageView I click. Next, I store the pictureSelected to an Intent extra but the result is the same.
Here is my code: (this is from function initView called in onCreate)
for(int i=0;i<3;i++){
ImageView temp = new ImageView(this);
String url = "/images/"+user.getEmail()+"_"+i+".jpg";
new ImageLoadTask(this,Constants.SERVER_URL+url,temp).execute();
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
temp.setLayoutParams(new ViewGroup.LayoutParams(width, height));
final int finalI = i;
temp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pictureSelected = finalI;
Intent intent = new Intent();
intent.putExtra("selected",pictureSelected);
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
Log.d("pict select",Integer.toString(pictureSelected));
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);
}
});
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.layoutProfilePictures);
linearLayout.addView(temp);
profilePictures.add(temp);
}
onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
Log.d("selected from result",Integer.toString(imageReturnedIntent.getIntExtra("selected",0)));
switch(requestCode) {
case REQ_CODE_PICK_IMAGE:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.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 filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
profilePictures.get(pictureSelected).setImageBitmap(yourSelectedImage);
user.getPhotoUri()[pictureSelected]=filePath;
Bitmap bm = BitmapFactory.decodeFile(filePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
String url = user.getEmail() + "_"+pictureSelected+".jpg";
InstagratApplication.getSocket().emit("upload image",url, encodedImage);
}
}
}
You can do this via setTag()
Here is example:
for(int i=0;i<3;i++){
ImageView temp = new ImageView(this);
String url = "/images/"+user.getEmail()+"_"+i+".jpg";
new ImageLoadTask(this,Constants.SERVER_URL+url,temp).execute();
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
temp.setLayoutParams(new ViewGroup.LayoutParams(width, height));
temp.setTag(url);
temp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String selectedPictureUrl = v.getTag();
Intent intent = new Intent();
intent.putExtra("selected",selectedPictureUrl);
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);
}
});
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.layoutProfilePictures);
linearLayout.addView(temp);
profilePictures.add(temp);
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments