问题
I have the codes work for loading image from gallery but I really do not understand how it works. Here are the codes.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { //Browse Gallery is requested
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
loadImage(picturePath); //load picture according the path
image_View.setImageBitmap(pic); //Show the selected picture
}
}
Uri selectedImage = data.getData();
Get the uri of selected image from intent
String[] filePathColumn = { MediaStore.Images.Media.DATA };
MediaStore.Images.Media.DATA is constant. I do not understand why do not use String instead of String[]
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
I do not understand this line.
cursor.moveToFirst();
Move to the first picture in Gallery.
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
For this one, I always get 0 no matter which picture I choose.
String picturePath = cursor.getString(columnIndex);
Since columnIndex always 0, then how can it get different path for different picture?
Can anyone help me to check whether my explanation is correct and explain the line that I do not understand? Thanks.
回答1:
1-
Uri selectedImage = data.getData();
This is the statement where you need to read the data passed through another intent which you called earlier via startActivityForResult
method. In this case probably you open an intent and let the user select an image, then the URI
of the image will be returned to you and you use getData
to read that.
2-
String[] filePathColumn = { MediaStore.Images.Media.DATA };
When you want cursors to read something a Content Provider (via ContentResolver
) you need to specify which columns you need to read from the database, and the argument you need to pass should be an array of String (whether it has one or more columns you still need to pass an array). MediaStore.Images.Media
is a Database Contract which contains constants which you need to use to talk to Content Providers
3-
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
Cursors are used for reading data from Content Providers. If you are familiar with other programming languages it is like reading rows from database and your results are stored in Cursors. When you pass URI, you don't need to specify which database to read, ContentResolver
will find that out for you (this is an advantage of using content providers)
4-
cursor.moveToFirst();
When you read the desired rows from the database (in this case probably you just selected one image), you need to move the cursor to point to the first entry (row) of the returned results
5-
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
You need to know which column you need to access to read your desired data (in this case file path name). so you ask cursor what is the column index of the file pathname
and it will return you the column index. and of course in this case it will be always 0 because you only asked the content provider to return one column (file pathname
) so there would be no more data to show other than that
6-
String picturePath = cursor.getString(columnIndex);
and finally this statement asks cursor to get the file pathname
located at the index (in this case index 0
) so at the end you have your path to file. Note that you can read only one picture data at a time with this method
回答2:
Cursors store query result records in rows and grant many methods to access and iterate through the records. Also int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
is the reason why you always get 0 for your index
来源:https://stackoverflow.com/questions/36466483/how-the-codes-work-for-loading-image-from-gallery-android