I'm trying to retrieve some information from my onActivityResult method, in my case is a picture the user has selected in a subactivity, i believe that is working fine, what i am having problems with is when i try to assign that picture (in form of a byte array) to a variable i have in my main activity. I'm trying to use that variable in a onClick method, but for some reason when i try to use that variable it's value is always null. what could be happening? here is the code:
poptions.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View view, int pos, long id) {
final Intent i;
switch(pos){
// Adjunta imagen
case 0:
i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("image/jpeg");
startActivityForResult(i,1);
//Toast.makeText(view.getContext(), "MIERDA!!", 30).show();
break; // more cases below, not relevant to my question....
}
}
});
my onActivityResult method:
EDIT: The solution was to change
if(resultCode==1)
to:
if(resultCode==RESULT_OK)
turns out, RESULT_OK actually equals -1.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent pic){
super.onActivityResult(requestCode, resultCode, pic);
if(resultCode==1){
Uri selectedpic = pic.getData();
try {
Bitmap bitmappic = MediaStore.Images.Media.getBitmap(
this.getContentResolver(),
selectedpic);
ByteArrayOutputStream picstream = new ByteArrayOutputStream();
bitmappic.compress(Bitmap.CompressFormat.JPEG, 100, picstream);
setPicture(picstream.toByteArray());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
my onClickListener method where i'm trying to use the picture that was set by my setPicture method above.
btnSend.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Bundle usrpostData = new Bundle();
if(txtMsg.getText().toString().equals("")){
Toast.makeText(v.getContext(), "MSG", 10).show();
}else{
String msg = txtMsg.getText().toString();
usrpostData.putString("message", msg);
if(getPicture()==null){
Toast.makeText(v.getContext(),"NULL",30).show();
}else{
try {
// do something with the picture...
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
As you can see i put a little test, and the getPicture method always return null, i tried .equals instead of == and it makes my app crash. any help is appreciated
The solution was to change
if(resultCode==1)
to:
if(resultCode==RESULT_OK)
turns out, RESULT_OK actually equals -1.