I found this issue only in some devices when trying to pick the picture for the profile image. While checking in the emulator these issue are not seen but on live version of
Try this:
public static final int KITKAT_VALUE = 1002;
Intent intent;
if (Build.VERSION.SDK_INT < 19) {
intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(intent, KITKAT_VALUE);
} else {
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(intent, KITKAT_VALUE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == KITKAT_VALUE ) {
if (resultCode == Activity.RESULT_OK) {
// do something here
}
}
}
I am trying to persist the Uri and use that Uri later
That will work for a Uri
with a file
scheme. It will not work for most Uri
values with a content
scheme. Those are more akin to an HTTP URL to an authenticated Web site — the URL is good while your session is alive but is useless afterwards.
If you obtain the Uri
via ACTION_OPEN_DOCUMENT
, ACTION_CREATE_DOCUMENT
, or ACTION_OPEN_DOCUMENT_TREE
, and you call takePersistableUriPermission()
on ContentResolver
supplying the Uri
, then you can safely persist that Uri
. You will have access to the content indefinitely, until the user revokes access or the content significantly changes (e.g., is deleted) such that its Uri
is no longer valid.
Any other content
Uri
(e.g., ACTION_GET_CONTENT
) is only good for a very short time. Your only option here is to copy the content to some file that you control (e.g., in getCacheDir()
) and save the path to that file.