Reputation: 1009
I had this code, without the bundle it works very well, but when I add the bundle in the intent, in my onActivityResult
I don't found this id_q
in data, I found just the path of the chosen picture.
Intent intent = new Intent();
Bundle b = new Bundle();
b.putInt("id_q", q.getId());
intent.putExtras(b);
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_IMAGE);
this is onActivityResult :
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_IMAGE)
onSelectFromGalleryResult(data);
and in onSelectFromGalleryResult(data) I had this :
if (data != null && data.getExtras() != null) {
Bundle b = data.getExtras();
int id_q = b.getInt("id_q");}
Upvotes: 0
Views: 57
Reputation: 141
According to the comments, I would suggest something like this.
public class YourActivity extends Activity {
ArrayList<Question> questionsLeft;
Question currentQ;
/* ... */
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_IMAGE) {
// Do what you have to do with data and currentQ
processNext();
}
}
}
public void processNext() {
if(questionsLeft.isEmpty()) {
return;
}
currentQ = questionsLeft.get(0);
questionsLeft.remove(0);
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_IMAGE);
}
}
You can also have one requestCode for each question in your list. You will know that your request is "SELECT_IMAGE" if it is in a given range instead of a single value.
Upvotes: 1
Reputation: 3101
If you really want to assign the id_q you can achieve it like this way.
Step 1: Create a global variable lets say int counter = 0; and also the arraylist or list whatever you taken for your ids. lets say for e.g.
ArrayList<Integer> myList;
Step 2: Now, Call your Intent to choose image inside for loop
For E.g
For loop (up to your n number of size)
{
// Now Call your intent here.
}
Step 3: Inside your OnActivityResult in your onSelectFromGalleryResult(data);
int id_q = yourArrayList.get(counter);
increase the size of Counter.
counter++;
Now, you don't need to do anything further once you pick one by one image from Gallery it comes to startActivityResult and you will get all the ids and your counter increases too so you got all the new values from arrayList from particular position.
I hope this will help you out.
Upvotes: 1
Reputation: 551
Try this.
There is no need of a Bundle here.
Intent intent = new Intent();
intent.putIntExtra("id_q", q.getId());
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_IMAGE);
and in onActivityResult
int id_q = data.getIntExtra("id_q");
Upvotes: -1
Reputation: 2170
On activity result is called in the activity from which you are calling the startActivityForResult();
As you are using the chooser in this case it is not necessary that the result intent will contain the same information as you are adding.
Hope this helps.
Upvotes: 0