溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android開發中怎么實現一個從圖庫中選取圖片的功能

發布時間:2020-11-20 16:50:55 來源:億速云 閱讀:246 作者:Leah 欄目:移動開發

Android開發中怎么實現一個從圖庫中選取圖片的功能?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。

android 從圖庫中選取圖片

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

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

<&#63;xml version="1.0" encoding="utf-8"&#63;> 
<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();獲得了圖庫中的圖片所有數據了。

看完上述內容,你們掌握Android開發中怎么實現一個從圖庫中選取圖片的功能的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女