关于拍照,相册 取照片

来源:互联网 发布:郭富城 小美 知乎 编辑:程序博客网 时间:2024/05/01 16:09

由于项目着急,总是拷贝代码,实现功能就行了,很少思考。闲着无聊就看了下代码。。

SD_CARD_TEMP_DIR = Environment.getExternalStorageDirectory()+ File.separator + "llkj" + File.separator + "my"+ File.separator + "ysh-" + "commodity" + ".jpg";String state = Environment.getExternalStorageState();if (state.equals(Environment.MEDIA_MOUNTED)) {file = new File(SD_CARD_TEMP_DIR);file.delete();if (!file.exists()) {try {file.createNewFile();} catch (IOException e) {e.printStackTrace();Toast.makeText(this, "照片创建失败!", Toast.LENGTH_LONG).show();return;}}Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));startActivityForResult(intent, 4);
BitmapFactory.Options option = new BitmapFactory.Options();<span style="white-space:pre"></span>option.inSampleSize = 2;<span style="white-space:pre"></span>Bitmap photo = BitmapFactory.decodeFile(f.getPath(), option);
//为什么要用路径呢,就是把拍照的照片存放到指定的路径,然后根据路径直接去取出来就行了。就这么简单。

if (data != null) {Bitmap photo = null;String[] projection;File f = null;Bundle extras = data.getExtras();if (null != extras) {photo = (Bitmap) extras.get("data");} else {Uri uri = data.getData();if (uri != null) {projection = new String[] { MediaColumns.DATA };Cursor cursor = managedQuery(uri, projection, null,null, null);int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);System.out.println("706-------------camera------"+ column_index);cursor.moveToFirst();String uploadPhotPath = cursor.getString(column_index);System.out.println("camera--------------------561----------- 相册图片的地址--------"+ uploadPhotPath);BitmapFactory.Options option = new BitmapFactory.Options();option.inSampleSize = 2;photo = BitmapFactory.decodeFile(uploadPhotPath, option);}}


//相册读取照片,就是返回uri根据uri找到照片就ok

不管拍照或者相册取出的照片剪切,返回的是bitmap直接用就行了

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);              photoPickerIntent.setType("image/*");              photoPickerIntent.putExtra("crop", "true");              photoPickerIntent.putExtra("aspectX", 1);              photoPickerIntent.putExtra("aspectY", 1);              photoPickerIntent.putExtra("outputX", 96);              photoPickerIntent.putExtra("outputY", 96);              photoPickerIntent.putExtra("return-data", true);              startActivityForResult(photoPickerIntent, 3);  

 Intent cropIntent = new Intent("com.android.camera.action.CROP");                  cropIntent.setDataAndType(Uri.fromFile(f), "image/*");                  cropIntent.putExtra("crop", "true");                  cropIntent.putExtra("aspectX", 1);                  cropIntent.putExtra("aspectY", 1);                  cropIntent.putExtra("outputX", 96);                  cropIntent.putExtra("outputY", 96);                  cropIntent.putExtra("return-data", true);                  startActivityForResult(cropIntent, 3);  
  case 3:                if (data != null) {                    final Bitmap photo = data.getParcelableExtra("data");                  if (photo == null)                      return;                  dialog = ProgressDialog.show(this, "", "正在上传", true, true);                  // 调用上传头像接口                  // uploadFile.uploadImg(getApplicationContext(), photo);                }  




0 0
原创粉丝点击