生活随笔
收集整理的這篇文章主要介紹了
Android学习笔记进阶十三获得本地全部照片
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
這是Intent的一個用法。
在ActivityAction里面有一個“ACTION_GET_CONTENT”字符串常量,該常量讓用戶選擇特定類型的數(shù)據(jù)。
intent.setType("image/*");? 選擇本地所有的圖片。
返回該數(shù)據(jù)的URI.我們利用該常量生成該圖片的位圖Bitmap,然后為添加到圖片控件(ImageView)上就行了。
選擇你想要的圖片:
?
?
main.xml
[java] view plaincopy
<?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"????????>???????<Button?????????????android:id="@+id/button"????????????android:layout_width="fill_parent"?????????????android:layout_height="wrap_content"?????????/>????????<ImageView????????????android:id="@+id/image"????????????android:layout_width="fill_parent"???????????android:scaleType="fitXY"????????????android:layout_height="wrap_content"??????????/>????</LinearLayout>???
?
[java] view plaincopy
package?xiaosi.image;????import?java.io.FileNotFoundException;????import?android.app.Activity;????import?android.content.ContentResolver;????import?android.content.Intent;????import?android.graphics.Bitmap;????import?android.graphics.BitmapFactory;????import?android.net.Uri;????import?android.os.Bundle;????import?android.util.Log;????import?android.view.View;????import?android.view.View.OnClickListener;??import?android.widget.Button;????import?android.widget.ImageView;????public?class?ImageActivity?extends?Activity?{????????????private?Button?button?=?null;??????private?ImageView?imageView?=?null;??????@Override????????public?void?onCreate(Bundle?savedInstanceState)?{????????????super.onCreate(savedInstanceState);????????????setContentView(R.layout.main);????????????button?=?(Button)findViewById(R.id.button);????????????button.setText("選擇圖片");????????????button.setOnClickListener(new?ButtonListener());????????}????????????private?class?ButtonListener?implements?OnClickListener{????????????public?void?onClick(View?v)??????????{??????????????Intent?intent?=?new?Intent();????????????????????????????intent.setType("image/*");????????????????/*?使用Intent.ACTION_GET_CONTENT這個Action?*/????????????????intent.setAction(Intent.ACTION_GET_CONTENT);?????????????????????????????startActivityForResult(intent,?1);????????????}???????}???????@Override?????????protected?void?onActivityResult(int?requestCode,?int?resultCode,?Intent?data)?{?????????????if?(resultCode?==?RESULT_OK)?{?????????????????Uri?uri?=?data.getData();?????????????????Log.e("uri",?uri.toString());?????????????????ContentResolver?contentResolver?=?this.getContentResolver();?????????????????try?{?????????????????????Bitmap?bitmap?=?BitmapFactory.decodeStream(contentResolver.openInputStream(uri));?????????????????????imageView?=?(ImageView)?findViewById(R.id.image);??????????????????????????????????????imageView.setImageBitmap(bitmap);?????????????????}????????????????catch?(FileNotFoundException?e){?????????????????????Log.e("Exception",?e.getMessage(),e);?????????????????}?????????????}?????????????super.onActivityResult(requestCode,?resultCode,?data);?????????}????}????
?
?
?
源代碼:點擊打開鏈接
轉(zhuǎn)載于:https://www.cnblogs.com/Free-Thinker/p/6722060.html
總結(jié)
以上是生活随笔為你收集整理的Android学习笔记进阶十三获得本地全部照片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。