1.android studioå¦ä½è°ç¨ç³»ç»ç¸å
android studioå¦ä½è°ç¨ç³»ç»ç¸å
å¨Android Studioä¸ï¼å¯ä»¥ä½¿ç¨Intentè°ç¨ç³»ç»ç¸åã以ä¸æ¯å ·ä½æ¥éª¤ï¼
1. å¨ä½ çAndroid Studio项ç®ä¸å建ä¸ä¸ªButtonæImageViewï¼ç¨äºè§¦åæå¼ç¸åçäºä»¶ã
2. å¨ButtonæImageViewçOnClickäºä»¶ä¸æ·»å 以ä¸ä»£ç ï¼
Intent intent = new Intent(Intent.ACTION_PICK,络相看懂vs源码 android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, PICK_IMAGE_REQUEST);
å ¶ä¸ï¼PICK_IMAGE_REQUESTæ¯ä¸ä¸ªæ´æ°å¸¸éï¼ç¨äºå¨Activityè¿åç»ææ¶è¿è¡è¯å«ã
3. å¨Activityä¸æ·»å 以ä¸ä»£ç ï¼ä»¥å¤çä»ç¸åè¿åçç»æï¼
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
// 使ç¨uriå è½½å¾ç
}
}
å¨è¿ä¸ªæ¹æ³ä¸ï¼æ们æ£æ¥requestCodeæ¯å¦çäºPICK_IMAGE_REQUESTï¼resultCodeæ¯å¦çäºRESULT_OKï¼ä»¥ådataådata.getData()æ¯å¦ä¸ä¸ºnullãå¦æè¿äºæ¡ä»¶é½æ»¡è¶³ï¼æ们就å¯ä»¥ä½¿ç¨data.getData()æ¹æ³è·åä»ç¸åä¸éæ©çå¾ççUriï¼å¹¶ä½¿ç¨è¿ä¸ªUriå è½½å¾çã
注æï¼å¨ä½¿ç¨æå¼ç¸åçIntentæ¶ï¼éè¦å¨AndroidManifest.xmlæ件ä¸æ·»å 以ä¸æéï¼
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
è¿ä¸ªæéç¨äºè¯»ååå¨å¨è®¾å¤ä¸çå¾çã