Reputation: 329
I have an intent that picks an image (I'm calling this in a fragment):
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/png");
intent.addCategory(Intent.CATEGORY_OPENABLE);
String[] mimetypes = {"image/png","image/jpg", "image/bmp", "image/gif"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
// special intent for Samsung file manager
Intent sIntent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
// if you want any file type, you can skip next line
sIntent.putExtra("CONTENT_TYPE", "image/png");
sIntent.addCategory(Intent.CATEGORY_DEFAULT);
Intent chooserIntent;
if (getContext().getPackageManager().resolveActivity(sIntent, 0) != null){
// it is device with Samsung file manager
chooserIntent = Intent.createChooser(sIntent, "Open file");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { intent});
} else {
chooserIntent = Intent.createChooser(intent, "Open file");
}
try {
startActivityForResult(chooserIntent, 548);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getContext(), "No suitable File Manager was found.", Toast.LENGTH_SHORT).show();
}
And in my activity:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(TAG, "onActivityResult: request code: " + requestCode);
Log.d(TAG, "onActivityResult: result: " + resultCode + " Success = " + (resultCode == RESULT_OK));
if (requestCode == 548 && resultCode == RESULT_OK) {
Uri uri = data.getData();
Log.d(TAG, "onActivityResult: URI: " + uri.toString());
Log.d(TAG, "onActivityResult: OK");
}
}
output: onActivityResult: request code: 66084 onActivityResult: result: -1 Success = true
As you can see, the result was successful but the request code changes. so i can't check what was the request.
Upvotes: 0
Views: 86
Reputation: 1006869
Your startActivityForResult()
call should be on the same thing that your onActivityResult()
is on.
So, if you want the onActivityResult()
to be on the activity, call startActivityForResult()
on the activity.
startActivityForResult()
on the fragment intentionally changes the requestCode
to minimize conflicts with other startActivityForResult()
calls, particularly those made on other fragments in the same activity.
Upvotes: 1