欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中從圖庫(kù)中選取圖片實(shí)例詳解

 更新時(shí)間:2017年01月24日 10:18:41   投稿:lqh  
這篇文章主要介紹了Android中從圖庫(kù)中選取圖片實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下

android 從圖庫(kù)中選取圖片

 在android中,如何從圖庫(kù)gallary中挑選圖片呢,其實(shí)很簡(jiǎn)單,步驟如下

1) 設(shè)計(jì)一個(gè)imageview,用來顯示圖庫(kù)選出來的圖片 

<?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) 學(xué)習(xí)如何在按鍵中調(diào)出gallary,其實(shí)也就是intent了,如下 

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

3) 然后在onActivityResult中對(duì)調(diào)出圖庫(kù)后,選定好的圖片,我們要重新顯示在頁(yè)面的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();獲得了圖庫(kù)中的圖片所有數(shù)據(jù)了。

  這樣一來,當(dāng)用戶在圖庫(kù)中選好圖片后,就可以呈現(xiàn)在imageview控件中咯

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

相關(guān)文章

最新評(píng)論