從相冊中選擇圖片進行壓縮并用ImageView展示出來
從手機中選擇照片這是幾乎所有應用的功能之一,主要考慮到一點的就是如果圖片太大了,可能會OOM,簡單的處理就是對圖片進行壓縮!
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/photo_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="選擇照片" />
<ImageView
android:id="@+id/photo_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="選擇自己相冊照片"
android:layout_gravity="center"
android:layout_marginTop="10dp"/>
</LinearLayout>
-----------操作代碼--------------
public class PhotoActivity extends Activity {
/**點擊選擇照片按鈕*/
private Button photoBtn;
/**選擇后展示照片*/
private ImageView photoIv;
/**請求碼*/
private final static int SELECT_PHOTO_CODE = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo);
initViews();
initListners();
}
private void initViews() {
photoBtn = (Button) findViewById(R.id.photo_btn);
photoIv = (ImageView) findViewById(R.id.photo_iv);
}
private void initListners() {
photoBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
selectPhotoFromAblum();
}
});
}
/**從相冊庫中選擇圖片操作*/
private void selectPhotoFromAblum() {
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent, SELECT_PHOTO_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case SELECT_PHOTO_CODE:
if (null != data) {
/**取得圖片,圖片壓縮比為4*4*/
Bitmap bm = compressBitmap(null, null, this, data.getData(), 4, false);
photoIv.setImageBitmap(bm);
}
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
/**圖片壓縮處理,size參數為壓縮比,比如size為2,則壓縮為1/4**/
private Bitmap compressBitmap(String path, byte[] data, Context context, Uri uri, int size, boolean width) {
Options options = null;
if (size > 0) {
Options info = new Options();
/**如果設置true的時候,decode時候Bitmap返回的為數據將空*/
info.inJustDecodeBounds = false;
decodeBitmap(path, data, context, uri, info);
int dim = info.outWidth;
if (!width) dim = Math.max(dim, info.outHeight);
options = new Options();
/**把圖片寬高讀取放在Options里*/
options.inSampleSize = size;
}
Bitmap bm = null;
try {
bm = decodeBitmap(path, data, context, uri, options);
}
catch (Exception e) {
e.printStackTrace();
}
return bm;
}
/**把byte數據解析成圖片*/
private Bitmap decodeBitmap(String path, byte[] data, Context context, Uri uri, BitmapFactory.Options options) {
Bitmap result = null;
if (path != null) {
result = BitmapFactory.decodeFile(path, options);
}
else if (data != null) {
result = BitmapFactory.decodeByteArray(data, 0, data.length, options);
}
else if (uri != null) {
ContentResolver cr = context.getContentResolver();
InputStream inputStream = null;
try {
inputStream = cr.openInputStream(uri);
result = BitmapFactory.decodeStream(inputStream, null, options);
inputStream.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
}
最后注意權限:
<!-- 從SDCard讀取數據權限 --><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>