国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android中從圖庫中選取圖片實例詳解

2019-12-12 03:53:24
字體:
來源:轉載
供稿:網友

android 從圖庫中選取圖片

 在android中,如何從圖庫gallary中挑選圖片呢,其實很簡單,步驟如下

1) 設計一個imageview,用來顯示圖庫選出來的圖片 

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   android:orientation="vertical"   android:layout_width="fill_parent"   android:layout_height="fill_parent"   >   <ImageView       android:id="@+id/imgView"       android:layout_width="fill_parent"       android:layout_weight="1" android:layout_height="wrap_content"></ImageView>   <Button        android:layout_height="wrap_content"        android:text="Load Picture"        android:layout_width="wrap_content"        android:id="@+id/buttonLoadPicture"        android:layout_weight="0"        android:layout_gravity="center"></Button> </LinearLayout> 

2) 學習如何在按鍵中調出gallary,其實也就是intent了,如下 

  Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(i, RESULT_LOAD_IMAGE); 

3) 然后在onActivityResult中對調出圖庫后,選定好的圖片,我們要重新顯示在頁面的imageview中,因此代碼如下: 

protected void onActivityResult(int requestCode, int resultCode, Intent data) {   super.onActivityResult(requestCode, resultCode, data);      if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {     Uri selectedImage = data.getData();     String[] filePathColumn = { MediaStore.Images.Media.DATA };      Cursor cursor = getContentResolver().query(selectedImage,         filePathColumn, null, null, null);     cursor.moveToFirst();      int columnIndex = cursor.getColumnIndex(filePathColumn[0]);     String picturePath = cursor.getString(columnIndex);     cursor.close();          ImageView imageView = (ImageView) findViewById(R.id.imgView);     imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));      } 

  其中就是Uri selectedImage = data.getData();獲得了圖庫中的圖片所有數據了。

  這樣一來,當用戶在圖庫中選好圖片后,就可以呈現在imageview控件中咯

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 拉萨市| 承德市| 中方县| 柳林县| 蚌埠市| 兴隆县| 九江市| 九台市| 普兰店市| 尚义县| 尼玛县| 尼勒克县| 隆尧县| 福贡县| 叶城县| 松溪县| 宾阳县| 云梦县| 留坝县| 阳曲县| 泰州市| 漾濞| 临高县| 南安市| 策勒县| 韶山市| 普陀区| 新龙县| 呼图壁县| 隆化县| 舟曲县| 文水县| 布尔津县| 富民县| 麻江县| 丹棱县| 红桥区| 嘉善县| 铜鼓县| 山阴县| 湘潭市|