There are many ways to obtain album pictures on the Internet by Android development. Here is a method after Android 4.4. Because the higher the version, some old APIs will be deprecated, and the new APIs and the old APIs are incompatible, resulting in many problems.
For example: managedQuery() has now been replaced by getContentResolver().query(), but their parameters are the same
For example, after Android 4.4, the pictures obtained by the two methods are different. The old method causes the pictures to be obtained in the Android 4.4 system.
private ImageView imgShow = null; private TextView imgPath = null; private final int IMAGE_CODE = 0; Uri bitmapUri = null; private final String IMAGE_TYPE = "image/*";
imgShow is an imageView control used to display images, imgPath is a TextView control used to display the path to the image, and all must be connected to the corresponding control Id in the onCreate() function. IMAGE_CODE is a custom parameter that can be used as other values.
private void selectImage() { // TODO Auto-generated method stub boolean isKitKatO = .SDK_INT >= Build.VERSION_CODES.KITKAT; Intent getAlbum; if (isKitKatO) { getAlbum = new Intent(Intent.ACTION_OPEN_DOCUMENT); } else { getAlbum = new Intent(Intent.ACTION_GET_CONTENT); } (IMAGE_TYPE); startActivityForResult(getAlbum, IMAGE_CODE); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode != RESULT_OK) { ("TAG->onresult", "ActivityResult resultCode error"); return; } Bitmap bm = null; ContentResolver resolver = getContentResolver(); if (requestCode == IMAGE_CODE) { try { Uri originalUri = (); //Get the uri of the picture bitmapUri = originalUri; isSelectPic = true; bm = (resolver, originalUri); //Import the bitmap image (bm); String[] proj = {}; Cursor cursor = getContentResolver().query(originalUri, proj, null, null, null); if(()) { int column_index = (); String path = (column_index); (path); } (); } catch (IOException e) { ("TAG-->Error", ()); } } }
Used here
getContentResolver().query() replaces the old managedQuery(), and at the beginning, it also determines whether the compiled SDK version is a version after Android 4.4 or later
boolean isKitKatO = .SDK_INT >= Build.VERSION_CODES.KITKAT;
If yes, use the new method, otherwise use the old method and call the selectImage() function in the button control to get the picture from the image library.
To get the corresponding bitmap image through Uil, you can use the following methods:
private Bitmap decodeUriAsBitmap(Uri uri) { Bitmap bitmap = null; try { bitmap = (getContentResolver().openInputStream(uri)); } catch (FileNotFoundException e) { (); return null; } return bitmap; }
By passing in the uri of the obtained image, you can get the corresponding bitmap image.
The above is all about this article, I hope it will be helpful for everyone to learn Android software programming.