从相机或者相册选择图片
2016-03-30 23:34
295 查看
</pre><pre name="code" class="html"><p><span style="white-space:pre"> </span>private static final int TAKE_PICTURE = 0; <span style="white-space:pre"> </span>private static final int CHOOSE_PICTURE = 1; <span style="white-space:pre"> </span>private static final int SCALE = 2; </p><p>/**</p> * 从相机或者图库选择图片 * * @param context */ public void showPicturePicker(Context context) { AlertDialog.Builder builder = new AlertDialog.Builder(context); // builder.setTitle("图片来源"); builder.setNegativeButton("取消", null); builder.setItems(new String[] { "拍照", "从相册选择" }, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case TAKE_PICTURE: Intent openCameraIntent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE); Uri imageUri = Uri.fromFile(new File(Environment .getExternalStorageDirectory(), "image.jpg")); // 指定照片保存路径(SD卡),image.jpg为一个临时文件,每次拍照后这个图片都会被替换 openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(openCameraIntent, TAKE_PICTURE); break; case CHOOSE_PICTURE: Intent openAlbumIntent = new Intent( Intent.ACTION_GET_CONTENT); openAlbumIntent.setType("image/*"); startActivityForResult(openAlbumIntent, CHOOSE_PICTURE); break; default: break; } } }); builder.create().show(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { switch (requestCode) { case TAKE_PICTURE: // 将保存在本地的图片取出并缩小后显示在界面上 Bitmap bitmap = BitmapFactory.decodeFile(Environment .getExternalStorageDirectory() + "/image.jpg"); Bitmap newBitmap = ImageTools.zoomBitmap(bitmap, bitmap.getWidth() / SCALE, bitmap.getHeight() / SCALE);//将图片缩放SCALE倍 // 由于Bitmap内存占用较大,这里需要回收内存,否则会报out of memory异常 bitmap.recycle(); // 将处理过的图片显示在界面上,并保存到本地 personal_head.setImageBitmap(ImageTools.toRoundBitmap(newBitmap)); // personal_head.setImageBitmap(newBitmap); ImageTools.savePhotoToSDCard(newBitmap, Environment .getExternalStorageDirectory().getAbsolutePath(), String.valueOf(System.currentTimeMillis())); sharedPreferencer.saveBitmapToSharedPreferences( PersonalDataActivity.this, ImageTools.toRoundBitmap(newBitmap)); break; case CHOOSE_PICTURE: ContentResolver resolver = getContentResolver(); // 照片的原始资源地址 Uri originalUri = data.getData(); try { // 使用ContentProvider通过URI获取原始图片 Bitmap photo = MediaStore.Images.Media.getBitmap(resolver, originalUri); if (photo != null) { // 为防止原始图片过大导致内存溢出,这里先缩小原图显示,然后释放原始Bitmap占用的内存 Bitmap smallBitmap = ImageTools.zoomBitmap(photo, photo.getWidth() / SCALE, photo.getHeight() / SCALE); // 释放原始图片占用的内存,防止out of memory异常发生 photo.recycle(); // personal_head.setImageBitmap(smallBitmap); personal_head.setImageBitmap(ImageTools .toRoundBitmap(smallBitmap)); sharedPreferencer.saveBitmapToSharedPreferences( PersonalDataActivity.this, ImageTools.toRoundBitmap(smallBitmap)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } break; default: break; } } }
/**
* 将图片保存在sharedpreferences
* @param context
* @param bitmap
*/
public static void saveBitmapToSharedPreferences(Context context,Bitmap bitmap){
// Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
//第一步:将Bitmap压缩至字节数组输出流ByteArrayOutputStream
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 80, byteArrayOutputStream);
//第二步:利用Base64将字节数组输出流中的数据转换成字符串String
byte[] byteArray=byteArrayOutputStream.toByteArray();
String imageString=new String(Base64.encodeToString(byteArray, Base64.DEFAULT));
//第三步:将String保持至SharedPreferences
SharedPreferences sharedPreferences=context.getSharedPreferences("imageSP", Context.MODE_PRIVATE);
Editor editor=sharedPreferences.edit();
editor.putString("image", imageString);
editor.commit();
}
/**
* 从sharedpreference中取出图片
* @param context
* @return
*/
public static Bitmap getBitmapFromSharedPreferences(Context context){
SharedPreferences sharedPreferences=context.getSharedPreferences("imageSP", Context.MODE_PRIVATE);
//第一步:取出字符串形式的Bitmap
String imageString=sharedPreferences.getString("image", "");
//第二步:利用Base64将字符串转换为ByteArrayInputStream
byte[] byteArray=Base64.decode(imageString, Base64.DEFAULT);
ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(byteArray);
//第三步:利用ByteArrayInputStream生成Bitmap
Bitmap bitmap=BitmapFactory.decodeStream(byteArrayInputStream);
return bitmap;
}
/** * 把正方形的图片转换成圆的 * @param bitmap * @return */ public static Bitmap toRoundBitmap(Bitmap bitmap) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); float roundPx; float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom; if (width <= height) { roundPx = width / 2; top = 0; bottom = width; left = 0; right = width; height = width; dst_left = 0; dst_top = 0; dst_right = width; dst_bottom = width; } else { roundPx = height / 2; float clip = (width - height) / 2; left = clip; right = width - clip; top = 0; bottom = height; width = height; dst_left = 0; dst_top = 0; dst_right = height; dst_bottom = height; } Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom); final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom); final RectF rectF = new RectF(dst); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, src, dst, paint); return output; }
相关文章推荐
- 战五渣前端,面试第一天总结
- Django-rest-framework学习(二)Request与Response
- Linux内核分析——第六周学习笔记
- Tomcat部署多个应用时日志文件路径相同引起的问题
- Dense prediction applications in Deep learning
- ListView实现分页功能
- bzoj1103[POI2007]大都市meg
- Android手机调用笔记本电脑的webservices
- 码农小汪-剑指Offer之3- 矩形覆盖
- 记毕业后第一次面试
- Android 使用SwipeBackLayout实现滑动返回上一级页面——实战来袭
- Android 使用SwipeBackLayout实现滑动返回上一级页面——实战来袭
- C++中的多态
- 网络是否可用
- js事件总结<未完----待续>
- 线程数究竟设多少合理
- 编译和链接
- opengl学习之一:opengl环境搭建
- Java实现排序算法2:堆排序
- 码农小汪-剑指Offer之2- 变态跳台阶