做过几次拍照,相册选取图片,但都记不住,这次发表个简单的保存下
private static final int PHOTO_GRAPH = 1;// 拍照 private static final int PHOTO_ALBUM = 2; // 相册获取 private static final String IMAGE_TYPE = "image/*";
1 public void takePhoto(){ 2 3 String SDState = Environment.getExternalStorageState(); 4 if(SDState.equals(Environment.MEDIA_MOUNTED)) 5 { 6 imgfileName = System.currentTimeMillis() + ".jpg"; 7 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 8 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File( 9 Environment.getExternalStorageDirectory(), imgfileName))); 10 intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0); 11 startActivityForResult(intent, PHOTO_GRAPH); 12 dialog.dismiss(); 13 }else{ 14 Toast.makeText(getThis(),"内存卡不存在", Toast.LENGTH_SHORT).show(); 15 } 16 }
拍照
//回调函数 protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data);if(resultCode == RESULT_OK){ switch (requestCode) { case NONE: break; case PHOTO_GRAPH://拍照 String myJpgPath = Environment.getExternalStorageDirectory()+"/"+imgfileName; if(ImageThumbnail.isFileExit(myJpgPath) == true){ img_SFZ.setImageBitmap(ImageThumbnail.getImageThumbnail(myJpgPath, img_SFZ.getWidth(), img_SFZ.getHeight())); } break; case PHOTO_ALBUM://相册获取 //照片的原始资源地址 Uri originalUri = data.getData(); try { //获取图片的路径: String[] proj = {MediaStore.Images.Media.DATA}; cursor = managedQuery(originalUri, proj, null, null, null); //按我个人理解 这个是获得用户选择的图片的索引值 int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); //最后根据索引值获取图片路径 String imgPath = cursor.getString(column_index); if (ImageThumbnail.isFileExit(imgPath) == true) { img_SFZ.setImageBitmap(ImageThumbnail.getImageThumbnail(imgPath, img_SFZ.getWidth(), img_SFZ.getHeight())); } } catch (Exception e) { // TODO: handle exception } break; default: break; } }
/** * 图片压缩显示 * @param imagePath * @param width * @param height * @return */ public static Bitmap getImageThumbnail(String imagePath, int width, int height) { Bitmap bitmap = null; BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; // 获取这个图片的宽和高,注意此处的bitmap为null bitmap = BitmapFactory.decodeFile(imagePath, options); options.inJustDecodeBounds = false; // 设为 false // 计算缩放比 int h = options.outHeight; int w = options.outWidth; int beWidth = w / width; int beHeight = h / height; int be = 1; if (beWidth < beHeight) { be = beWidth; } else { be = beHeight; } if (be <= 0) { be = 1; } options.inSampleSize = be; // 重新读入图片,读取缩放后的bitmap,注意这次要把options.inJustDecodeBounds 设为 false bitmap = BitmapFactory.decodeFile(imagePath, options); // 利用ThumbnailUtils来创建缩略图,这里要指定要缩放哪个Bitmap对象 bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT); return bitmap; }
图片压缩显示
时间: 2024-09-30 14:09:48