I'm new to android, and I was looking at how to retrieve a picture from a gallery, but I have few questions regarding the following code:
static final int REQUEST_IMAGE_GET = 1;
public void selectImage() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_IMAGE_GET);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_GET && resultCode == RESULT_OK) {
Bitmap thumbnail = data.getParcelable("data");
Uri fullPhotoUri = data.getData();
// Do work with photo saved at fullPhotoUri
...
}
}
In this line:Bitmap thumbnail = data.getParcelable("data");
First: Where did "data" key come from ?
Second: Why using Parcelable, why not using HasExtra("data")
Third: What is the purpose of having a bitmap and a URI together? I understand the URI to get it's location, but if I have it's bitmap. What's the importance of the URI and vice versa
Forth: What is the difference between getParcelableExtra()
and getParcelable()
Fifth: When do i call onActivityResult
?
So there are quite a few questions here!!!
the easiest Intent I know to let the user pick an image from gallery is:
public static final int GALLERY_REQUEST = 1001;
Intent galleryIntent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent , GALLERY_REQUEST );
and then you can get the image Uri selected by the user in this way:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_REQUEST && data != null && data.getData() != null){
//this is the uri of the image use it to load the image
Uri imageUri = intent.getData();
}
}
First: where did "data" key come from?
intent.getData()
is the key that android provides itself for to use the content that has been chosen (image, music and ...)
Second: why using Parcelable ,why not using HasExtra("data")
Parcelable is the way that android serializes object from one process to another. basically, when you call an intent and would like to pass some data object, the object must implement Parcelebale interface.so on an intent object you can check if the intent has a extra object with the object key name and using intent.hasExtra("keyname")
method. when you want to retrieve the Parcelable object from Intent you can simply use getParcelableExtra("keyname")
on the intent or you can get all the extras of that intent using getExtra()
which returns a bundle and then on the bundle you can call getParcelable("keyname")
to get the same Parcelable object
Third: what is the purpose of having a bitmap and a URI together? i understand the URI to get its location,but if i have it's bitmap ..whats the importance of the URI and vice versa
android won't give you the bitmap just a Uri. you will decide what to do with that Uri either load a Bitmap out of it or create a File.
Fifth: when do i call onActivityResult?
when ever you start an activity (basically using startActivityForResult(intent)
) and expecting some result from it ie: calling gallery and you want the gallery activity to return you a picture, you have to implement onActivityResult
method so android can call it and provide you with the requested result (if there is any)
hope it helps happy coding