Implementing method for android to get album pictures and paths

  • 2021-06-29 12:09:37
  • OfStack

There are many ways for Android to develop photo album pictures on the Internet. Here's a post-Android4 method. The higher the version, the older api will be discarded. The incompatibility between the new api and the old api will cause many problems.

For example, managedQuery () has now been replaced by getContentResolver (). query (), but their parameters are the same

Another example is Intent after Android4.4 (Intent.ACTION_GET_CONTENT;And Intent (Intent.ACTION_OPEN_DOCUMENT;The uri of the pictures obtained by the two methods are different, and the old method results in the picture not being available in the Android4.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 for displaying pictures. imgPath is an TextView control for displaying the path to the pictures. It is connected to the corresponding control Id, IMAGE_in the onCreate() function.CODE is a custom parameter that can be used for other values.


private void selectImage() {
  // TODO Auto-generated method stub
  boolean isKitKatO = Build.VERSION.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);
  }
  getAlbum.setType(IMAGE_TYPE);

  startActivityForResult(getAlbum, IMAGE_CODE);


 }

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

  if (resultCode != RESULT_OK) {

   Log.e("TAG->onresult", "ActivityResult resultCode error");

   return;

  }
  Bitmap bm = null;
  ContentResolver resolver = getContentResolver();
  if (requestCode == IMAGE_CODE) {
   try {

    Uri originalUri = data.getData();  // Getting Pictures uri
    bitmapUri = originalUri;
    isSelectPic = true;
    bm = MediaStore.Images.Media.getBitmap(resolver, originalUri);
    // Appear to bitmap picture 
    imgShow.setImageBitmap(bm);
    String[] proj = {MediaStore.Images.Media.DATA};

    Cursor cursor = getContentResolver().query(originalUri, proj, null, null, null);
    if(cursor.moveToFirst()) {
     int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
     String path = cursor.getString(column_index);
     imgPath.setText(path);
    }
    cursor.close();
   } catch (IOException e) {
    Log.e("TAG-->Error", e.toString());
   }

  }

 }

Use here

getContentResolver (). query () replaces the old managedQuery () and begins to determine at 1 whether the compiled version of sdk is later than Android 4.4
boolean isKitKatO = Build.VERSION.SDK_INT > = Build.VERSION_CODES.KITKAT;
Yes, use the new method, otherwise you can get pictures from the picture library by calling the selectImage() function in the button control, using the old method.
To get bitmap pictures from uil, you can do the following:


private Bitmap decodeUriAsBitmap(Uri uri) {
  Bitmap bitmap = null;
  try {
   bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
  } catch (FileNotFoundException e) {
   e.printStackTrace();
   return null;
  }
  return bitmap;
 }

Pass in the uri of the obtained picture to get the corresponding bitmap picture.

This is the whole content of this article, and I hope it will be helpful for everyone to learn Android software programming.


Related articles: