专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > 图形/图像

从相本中选择图片进行压缩并用ImageView展示出来

发布时间:2011-06-27 19:28:30 文章来源:www.iduyao.cn 采编人员:星星草
从相册中选择图片进行压缩并用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"/>

友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: