Android get photo and path implementation method _android

Source: Internet
Author: User

Android Development Get photo album of the way There are many kinds of online, here is a Android4.4 method, because the higher the version, some old APIs will be discarded, the new API and the old API is incompatible, resulting in a lot of problems.

For example: Managedquery () has now been replaced by Getcontentresolver (). query (), but their parameters are the same

such as Android4.4 after Intent (intent.action_get_content) and Intent (intent.action_open_document); two methods The URI of the picture is different, The old method causes the picture to be not obtained 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 a ImageView control used to display pictures, Imgpath is used to display TextView controls that get to the path to the picture, and to connect to the corresponding control in the OnCreate () function Id,image_code is a custom parameter. Can be a different number.

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!=) {L

   OG.E ("Tag->onresult", "Activityresult resultcode error");

  Return
  Bitmap BM = NULL;
  Contentresolver resolver = Getcontentresolver ();  if (Requestcode = = Image_code) {try {Uri Originaluri = Data.getdata ();
    Get the URI of the picture 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 ());

 }

  }

 }

Here with

Getcontentresolver () instead of the old Managedquery (), also at the outset to determine whether the compiled SDK version is a Android4.4 version
Boolean Iskitkato = Build.VERSION.SDK_INT >= build.version_codes. KitKat;
Yes, that's the new way, otherwise, using the old method, calling the Selectimage () function in the button control gets the picture from the picture library.
To get the corresponding bitmap picture through UIL, you can use the following methods:

Private Bitmap Decodeuriasbitmap (Uri uri) {
  Bitmap Bitmap = null;
  try {
   bitmap = Bitmapfactory.decodestream (Getcontentresolver (). Openinputstream (URI));
  FileNotFoundException e) {
   e.printstacktrace ();
   return null;
  }
  return bitmap;
 }

The URI of the captured picture can get the corresponding bitmap picture.

This is the entire content of this article, I hope to learn more about Android software programming help.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.