During the learning of Android, all through the death of a URL to get a picture, but how to achieve in the absence of a picture URL by clicking on the image to get the URL, this problem has been troubling the author, today there is time to check the online a lot of information, after repeated testing finally succeeded, Now share the code as follows. Due to the author's limited ability, mistakes are unavoidable, welcome to correct, such as this article violated your interests, please contact me. Thank you!
Environment: Android studio+genymotion
01. Layout:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " xmlns:tools=" Http://schemas.android.com/tools " android:layout_width=" Match_parent " android:layout_height= "Match_parent" android:orientation= "vertical" tools:context= " Www.itcast.com.testchoicepic.MainActivity "> <button android:id=" @+id/btn_select_pic " Android : layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "select Picture"/> < ImageView android:id= "@+id/iv_show_pic" android:layout_width= "wrap_content" android:layout_ height= "Wrap_content" android:src= "@mipmap/ic_launcher"/></linearlayout>
02. Main Code:
public class Mainactivity extends Appcompatactivity implements View.onclicklistener {private Button btn_select_pic; Private ImageView iv_show_pic; The request code, which is used to compare with the Requestcode in Onactivityresult, determines whether the same request public static final int request_code = 0; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Initview (); } private void Initview () {btn_select_pic = (Button) Findviewbyid (r.id.btn_select_pic); Iv_show_pic = (ImageView) Findviewbyid (r.id.iv_show_pic); Btn_select_pic.setonclicklistener (this); } @Override public void OnClick (View v) {switch (V.getid ()) {r.id.btn_select_pic: Intent Intent = new Intent (Intent.action_pick, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); Startactivityforresult (Intent, Request_code); Break }} @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onactivityresult (Requestcode, ResultCode, D ATA); Attention to judgment: ResultCode = = Result_ok if (Requestcode==request_code&&resultcode = = RESULT_OK && data! = Nu ll) {Uri Selectimageurl = Data.getdata (); String[] Filepathcolumn = {MediaStore.Images.Media.DATA}; cursor cursor = getcontentresolver (). Query (Selectimageurl, filepathcolumn, NULL, NULL, NULL); Cursor.movetofirst (); String Picpath = cursor.getstring (Cursor.getcolumnindex (filepathcolumn[0])); Cursor.close (); try {//1. Use Native Picture Display method Bitmap Bitmap = Bitmapfactory.decodefile (Picpath); Iv_show_pic.setimagebitmap (bitmap); 2. It is also possible to use a third-party framework, but you need to add a dependency in App/build.gradle: Compile ' com.github.bumptech.glide:glide:3.7.0 '/* glide . with (this)//specify context.Load (Picpath)//The URL of the specified picture. Placeholder (r.mipmap.ic_launcher)//Specifies the picture that is displayed before the picture is loaded successfully . Error (R.mipmap.ic_launcher)//Specifies the picture that the picture loading failed to display. Skipmemorycache (TRUE)//Skip Memory Cache . Diskcachestrategy (Diskcachestrategy.none)//Skip disk cache. Diskcachestrategy (Diskcachestrategy.source)/ /Just cache the original full-resolution image. Diskcachestrategy (Diskcachestrategy.result)//Just cache the final image. Di Skcachestrategy (Diskcachestrategy.all)//caches all versions of the image. Priority (Priority.high)//Specifies the precedence. Glide will use them as a guideline and handle these requests as much as possible, but it does not guarantee that all images will be loaded in the order required. Priority ordering: IMMEDIATE > High > NORMAL > Low. into (iv_show_pic);//Specify imageview*/} to display the picture catch (Exception e) {e.printstacktrace (); } } }}
03.:
Click on the phone library image to get to the clicked image