Android開發中怎么實現一個從圖庫中選取圖片的功能?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
海州網站制作公司哪家好,找成都創新互聯公司!從網頁設計、網站建設、微信開發、APP開發、響應式網站等網站項目制作,到程序開發,運營維護。成都創新互聯公司2013年至今到現在10年的時間,我們擁有了豐富的建站經驗和運維經驗,來保證我們的工作的順利進行。專注于網站建設就選成都創新互聯公司。
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();獲得了圖庫中的圖片所有數據了。
看完上述內容,你們掌握Android開發中怎么實現一個從圖庫中選取圖片的功能的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注創新互聯行業資訊頻道,感謝各位的閱讀!
新聞名稱:Android開發中怎么實現一個從圖庫中選取圖片的功能
網站網址:http://vcdvsql.cn/article34/jhehpe.html
成都網站建設公司_創新互聯,為您提供企業建站、商城網站、網頁設計公司、網站改版、全網營銷推廣、微信小程序
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯