This example for you to share the Android micro-letter selection of the image of the specific code, and micro-letter photo function for your reference, the specific contents are as follows
1.android6.0 system, for the use of permissions are required to apply, choose pictures and photos need to apply for Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_ Storage these two permissions.
if (Contextcompat.checkselfpermission (this, Manifest.permission.CAMERA)!= packagemanager.permission_granted) {
activitycompat.requestpermissions (activity) This,
new string[] {Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE},
request_storage_read_access_permission);
2. Through the image selector Multiimageselector to manage: Select mode, the maximum number of choices, whether to start the camera and other functions.
3. Click the picture selection button to jump to the Multiimageselectoractivity class, its layout is as follows: (a Toobar + a framelayout)
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "xmlns:app=" Http://schemas.android.com/apk/res-auto "android:orientation=" vertical "android:background=" 181819 "android:layout_width=" match_parent "android:layout_height=" Match_parent "> < Android.support.v7.widget.Toolbar android:id= "@+id/toolbar" android:layout_width= "Match_parent" Android:layout_ height= "Wrap_content" android:background= "@color/mis_actionbar_color" App:theme= "@style ThemeOverlay.AppCompat.Dark.ActionBar "android:minheight=" "Android:attr/actionbarsize" > <button android:id= "@+id/commit" android:background= "@drawable/mis_action_btn" android:minheight= "1DP" android:minwidth= "1DP" and
roid:layout_marginright= "16DP" android:paddingleft= "10DP" android:paddingright= "10DP" android:paddingtop= "5DP" Android:paddingbottom= "5DP" android:textcolor= "@color/mis_default_text_color" android:textsize= "14sp "android:layout_gravity=" right "android:layout_width=" Wrap_content "android:layout_height=" Wrap_content "/>
; </android.support.v7.widget.Toolbar> <framelayout android:id= "@+id/image_grid" android:layout_width= "
Match_parent "android:layout_height=" match_parent "/> </LinearLayout>
4. Invoke the following method to populate the fragment (multiimageselectorfragment) of the display picture.
Getsupportfragmentmanager (). BeginTransaction ()
. Add (R.id.image_grid, fragment.instantiate (this, MultiImageSelectorFragment.class.getName (), bundle))
. commit ();
5.MultiImageSelectorFragment layout use GridView to display pictures obtained from photo albums
<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/" Android "xmlns:tools=" Http://schemas.android.com/tools "android:background=" @android: Color/black "Android:layout_ Width= "Match_parent" android:layout_height= "match_parent" > <gridview android:id= "@+id/grid" Android:layout_w
Idth= "Match_parent" android:layout_height= "match_parent" android:horizontalspacing= "@dimen/mis_space_size" android:verticalspacing= "@dimen/mis_space_size" android:paddingbottom= "? Android:attr/actionbarsize" Android: Cliptopadding= "false" android:numcolumns= "3"/> <relativelayout android:clickable= "true" android:id= "@+id/foo" ter "android:background=" #cc000000 "android:layout_alignparentbottom=" true "android:layout_width=" Match_parent "an droid:layout_height= "Android:attr/actionbarsize" > <button android:id= "@+id/category_btn" android:paddingLe ft= "16DP" android:paddingright= "16DP" android:layouT_centervertical= "true" android:textcolor= "@color/mis_folder_text_color" tools:text= "All Pictures" android:textsize= "16sp "Android:gravity=" center_vertical "android:drawableright=" @drawable/mis_text_indicator "android:drawablepadding= "5DP" android:background= @null "android:singleline=" true "android:ellipsize=" End "Android:layout_width=" WRAP_
Content "android:layout_height=" match_parent "/> </RelativeLayout> </RelativeLayout>
6 Call the Loadercallbacks method inside the Android.support.v4.app.LoaderManager.class class, and then set the data to Mimageadapter after the loading is complete.
Mimageadapter.setdata (images);
7. When allowed to take pictures, display the photo button, call the System camera function.
Mgridview.setonitemclicklistener (New Adapterview.onitemclicklistener () {
@Override public
void Onitemclick (adapterview<?> Adapterview, view view, int I, long l) {
if (Mimageadapter.isshowcamera ()) {
if (i = = 0) {
showcameraaction ();
} else {
image image = (image ) Adapterview.getadapter (). GetItem (i);
Selectimagefromgrid (image, mode);
}
} else {
image image = (image) Adapterview.getadapter (). GetItem (i);
Selectimagefromgrid (image, mode);
}
}
);
Calling Camera Features
/** * Open camera */private void showcameraaction () {if (Contextcompat.checkselfpermission getcontext (), Manifes t.permission.write_external_storage)!= packagemanager.permission_granted) {requestpermission (Manifest.permission . Write_external_storage, GetString (r.string.mis_permission_rationale_write_storage), request_storage_write_access
_permission);
}else {Intent Intent = new Intent (mediastore.action_image_capture); if (Intent.resolveactivity (Getactivity (). Getpackagemanager ())!= null) {try {mtmpfile = Fileutils.createtmpfile
(Getactivity ());
catch (IOException e) {e.printstacktrace (); } if (mtmpfile!= null && mtmpfile.exists ()) {Intent.putextra (Mediastore.extra_output, Uri.fromfile (mTmp
File));
Startactivityforresult (Intent, Request_camera);
else {Toast.maketext (getactivity (), R.string.mis_error_image_not_exist, Toast.length_short). Show (); } else {Toast.maketext (getactivity (), R.string.mis_msg_no_camera, Toast.length_short). Show ();
}
}
}
Select picture
/**
* Notify callback
* @param image Image Data
*
/private void Selectimagefromgrid (image image, int mode {
if (image!= null) {
if (mode = = Mode_multi) {
if (Resultlist.contains (Image.path)) {
Resultlist.remove (Image.path);
if (mcallback!= null) {
mcallback.onimageunselected (image.path);
}
} else {
if (selectimagecount () = = Resultlist.size ()) {
Toast.maketext (getactivity (), R.string.mis_msg_amount_limit, Toast.length_short). Show ();
return;
}
Resultlist.add (Image.path);
if (mcallback!= null) {
mcallback.onimageselected (image.path);
}
}
Mimageadapter.select (image);
} else if (mode = = Mode_single) {
if (mcallback!= null) {
mcallback.onsingleimageselected (image.path);
}
}
}
}
This article has been organized into the "Android micro-credit Development tutorial Summary," Welcome to learn to read.
SOURCE Download: Http://xiazai.jb51.net/201611/yuanma/AndroidselectPicture (jb51.net). rar
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.