Android takes an instance code of a picture from the system gallery _android

Source: Internet
Author: User

This article illustrates how Android takes pictures from the system gallery. Share to everyone for your reference. Specifically as follows:

In your own application, take pictures from the system gallery and intercept them, and then return to your application. This is a lot of information about the application needs of the image function.

Write an example, come up with a big button, even the layout is not. Finally, select a part of the picture to use as the background of the button.
Here are a few things to note:

① from the gallery to select the saved picture clips, need to be saved in the SD card directory, can not be saved in the application of their own directory in memory, because it is the system diagram library to save this file, it does not have access to your application permissions;
②intent.putextra ("Crop", "true") to the clip's small box, otherwise there is no clip function, can only select pictures;
③intent.putextra ("Aspectx", 1) is the ratio of the clipping boxes that can be used to force the picture's aspect ratio.

The effect chart is as follows:

The Java code is as follows:

Package com.easymorse.gallery;
Import Java.io.File;
Import android.app.Activity;
Import android.content.Intent;
Import android.graphics.drawable.Drawable;
Import Android.net.Uri;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
 public class Galleryactivity extends activity {private static int select_picture;
 Private File tempfile;
 Button button; /** called the activity is a.
  * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  This.tempfile=new File ("/sdcard/a.jpg");
  button = New button (this);
  Button.settext ("Get Picture"); Button.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {Intent Intent = new in
    Tent (intent.action_get_content);
    Intent.settype ("image/*");
    Intent.putextra ("Crop", "true");
    Intent.putextra ("Aspectx", 1);
    Intent.putextra ("Aspecty", 2); Intent.putextra ("OUtput ", Uri.fromfile (Tempfile));
    Intent.putextra ("OutputFormat", "JPEG");
   Startactivityforresult (Intent.createchooser (Intent, "select Picture"), select_picture);
  }
  });
 Setcontentview (button); @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {if (ResultCode = = Result_o K) {if (Requestcode = = select_picture) {button.setbackgrounddrawable (Drawable.createfrompath tempFile. GetA
   Bsolutepath ()));

 }
  }
 }
}

I hope this article will help you with your Android program.

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.