Android中從圖庫中選取圖片實(shí)例詳解
android 從圖庫中選取圖片
在android中,如何從圖庫gallary中挑選圖片呢,其實(shí)很簡單,步驟如下
1) 設(shè)計(jì)一個(gè)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) 學(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中對調(diào)出圖庫后,選定好的圖片,我們要重新顯示在頁面的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();獲得了圖庫中的圖片所有數(shù)據(jù)了。
這樣一來,當(dāng)用戶在圖庫中選好圖片后,就可以呈現(xiàn)在imageview控件中咯
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- Android自定義ImageView實(shí)現(xiàn)在圖片上添加圖層效果
- Android 實(shí)現(xiàn)局部圖片滑動指引效果
- Android ViewPager實(shí)現(xiàn)圖片輪翻效果
- Android編程實(shí)現(xiàn)支持拖動改變位置的圖片中疊加文字功能示例
- Android中超大圖片無法顯示的問題解決
- Android自定義組件獲取本地圖片和相機(jī)拍照圖片
- android自定義ImageView仿圖片上傳示例
- Android ViewPager加載圖片效果
- Android百度地圖應(yīng)用之圖層展示
- Android編程實(shí)現(xiàn)圖片背景漸變切換與圖層疊加效果
相關(guān)文章
Android中在WebView里實(shí)現(xiàn)Javascript調(diào)用Java類的方法
這篇文章主要介紹了Android中在WebView里實(shí)現(xiàn)Javascript調(diào)用Java類的方法,本文直接給出示例,需要的朋友可以參考下2015-03-03
Flutter?點(diǎn)擊兩次退出app的實(shí)現(xiàn)示例
本文主要介紹了Flutter?點(diǎn)擊兩次退出app的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
Jetpack Compose實(shí)現(xiàn)對話框和進(jìn)度條實(shí)例解析
對話框和進(jìn)度條其實(shí)并無多大聯(lián)系,放在一起寫是因?yàn)閮烧叩膬?nèi)容都不多,所以湊到一起,對話框是我們平時(shí)開發(fā)使用得比較多的組件,進(jìn)度條的使用頻率也很高,比如下載文件,上傳文件,處理任務(wù)時(shí)都可以使用進(jìn)度條2023-04-04
Android10填坑適配指南(實(shí)際經(jīng)驗(yàn)代碼)
這篇文章主要介紹了Android10填坑適配指南(實(shí)際經(jīng)驗(yàn)代碼),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Android RecyclerView實(shí)現(xiàn)水平、垂直方向分割線
這篇文章主要為大家詳細(xì)介紹了Android RecyclerView實(shí)現(xiàn)水平、垂直方向分割線,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07

