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.