Ok so i have 3 ImageViews who load image from gallery when i click them.
My problem is , how to set the Image that i have loaded in the specific ImageView .
Im facing some issue with the ActivityForResult code (when load an Image and set it in ImageView1 for exemple , All ImageView take the same image instantly)
The full code :
package kane.navigationdrawer.activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
import kane.navigationdrawer.R;
import static android.R.attr.data;
import static kane.navigationdrawer.R.id.spinner;
import static kane.navigationdrawer.R.id.spinner4;
public class FormActivity extends AppCompatActivity {
private Spinner spinner1;
Button b1;
ImageView img1,img2,img3;
private static int RESULT_LOAD_IMAGE = 1;
private static int RESULT_LOAD_IMAGE2 = 1;
private static int RESULT_LOAD_IMAGE3 = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form);
Spinner spinner = (Spinner) findViewById(R.id.spinner4);
Spinner spinner2 = (Spinner) findViewById(R.id.spinner);
img1 = (ImageView) findViewById(R.id.img1);
img2 = (ImageView) findViewById(R.id.img2);
img3 = (ImageView) findViewById(R.id.img3);
spinner2.setAdapter(
new ArrayAdapter<CharSequence>(this,
R.layout.custom_spinner_item,
FormActivity.this.getResources().getTextArray(R.array.cat_arrays)));
spinner.setAdapter(
new ArrayAdapter<CharSequence>(this,
R.layout.custom_spinner_item,
FormActivity.this.getResources().getTextArray(R.array.vil_arrays)));
img1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent loadIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(loadIntent, RESULT_LOAD_IMAGE);
}
});
img2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent loadIntent2 = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(loadIntent2, RESULT_LOAD_IMAGE2);
}
});
img3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent loadIntent3 = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(loadIntent3, RESULT_LOAD_IMAGE3);
}
});
}
@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();
img1.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();
img2.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();
img3.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
Need your help , Thanks
You defined three request codes, but they all have the same value, hence all three if statements will be executed regardless of your image choice.
Simply assigning different values to your request code variables should fix the problem.
private static int RESULT_LOAD_IMAGE = 1;
private static int RESULT_LOAD_IMAGE2 = 2;
private static int RESULT_LOAD_IMAGE3 = 3;