Android 调用相机APP获取拍照图片和从相册选择图片

                       

源码部分分析

Camera.java → 点我定位,注意研究1134行与2023行的两个方法。

通过源码可以发现,输出的图片有2个分支

如果你没有指定Intent里面的Extra参数,它就返回一个序列化(putExtra(“data”, bitmap))的Bitmap,从理论上来说,这样的代码写的很烂,属于Magic Number。

如果你指定了Intent里面的Extra参数MediaStore.EXTRA_OUTPUT,拍照后它就直接把bitmap写到了Uri里面了,返回是空

一、获得拍照的预览图

使用范围:获得很小的预览图,用于设置头像等地方。
返回示例:bitmap = data.getExtras().getParcelable(“data”);

public final static int REQUEST_IMAGE_CAPTURE = 1;//startIntent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);//receive@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {    super.onActivityResult(requestCode, resultCode, data);    if (resultCode != RESULT_OK) {      Log.d(TAG, "canceled or other exception!");      return;    }    if (requestCode == REQUEST_IMAGE_CAPTURE) {      Log.d(TAG, "REQUEST_IMAGE_CAPTURE");      Bitmap bitmap;      try {        //"data"这个居然没用常量定义,也是醉了,我们可以发现它直接把bitmap序列化到intent里面了。        bitmap = data.getExtras().getParcelable("data");         //TODO:do something with bitmap, Do NOT forget call Bitmap.recycler();        mCameraImageview.setImageBitmap(bitmap);      } catch (ClassCastException e){        //do something with exceptions        e.printStackTrace();      }     }  }
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

二、获得原始的拍照文件

返回示例:

outputFileUri = file:///storage/sdcard0/JPEG_20150226_221320_-328629202.jpg
   
   
  • 1

使用范围:用于处理大的图片,比如使用滤镜,上传原始图像等操作,注意Uri不要用data私有目录,否则相机是写不进去的。

Android N 以后的设备 ,在传递uri到其他app的时候,需要使用FileProvider,参考这篇文章

public final static int REQUEST_IMAGE_CAPTURE = 1;Uri outputFileUri;//start  @OnClick(R.id.itemSelectCamera) void itemSelectCamera() {    File file = FileUtils.createImageFile();    outputFileUri = Uri.fromFile(file);    Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);    startActivityForResult(captureIntent, REQUEST_IMAGE_CAPTURE);  }//receive@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {    super.onActivityResult(requestCode, resultCode, data);    if (resultCode != RESULT_OK) {      Log.d(TAG, "canceled or other exception!");      return;    }    if (requestCode == REQUEST_IMAGE_CAPTURE) {      Log.d(TAG, "REQUEST_IMAGE_CAPTURE");      //TODO:Use the Uri       Intent intent = new Intent(this, ImageFilterActivity.class);      intent.setData(outputFileUri);      startActivity(intent);    }  }
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

关于文件如何创建,目前我找到的就是这个最稳定了,写到SD卡根目录(每个手机位置都不一样,坑太多了),再说一遍,data目录(Context.getXXDir())是私有目录,其它程序(比如Camera)是写不进去的

public class FileUtils {  public static File createImageFile() {    // Create an image file name    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());    String imageFileName = "JPEG_" + timeStamp + "_";    try {      File image = File.createTempFile(imageFileName,  /* prefix */          ".jpg",         /* suffix */          Environment.getExternalStorageDirectory()      /* directory */);      return image;    } catch (IOException e) {      //do noting      return null;    }  }}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

三、获取Gallery里面的图片

一、调用系统相册App,浏览所用图片

 Intent intent = new Intent();        intent.setAction(Intent.ACTION_VIEW);        intent.setType("image/*");        startActivity(intent);
   
   
  • 1
  • 2
  • 3
  • 4

二、调用系统相册,并从中选择一张照片

Intent intent = new Intent();        intent.setAction(Intent.ACTION_GET_CONTENT);        intent.setType("image/*");        startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
   
   
  • 1
  • 2
  • 3
  • 4

三、调用系统相册查看单张(或特定)图片

 //下方是将ImageList集合中的图片路径转换为可供File识别的String数据,    String value = String.valueOf(mImagesList.get(pos).getPicturePath());    File file = new File(value);    //下方是是通过Intent调用系统的图片查看器的关键代码    Intent intent = new Intent();    intent.setAction(android.content.Intent.ACTION_VIEW);    intent.setDataAndType(Uri.fromFile(file), "image/*");    startActivity(intent);
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

四、 Android 图片裁剪

//裁剪拍照后得到的图片    private void cropImageUri(Uri uri, int requestCode) {        Intent intent = new Intent("com.android.camera.action.CROP");        intent.setDataAndType(uri, "image/*");        //intent.putExtra("crop", "true");        intent.putExtra("aspectX", 1);        intent.putExtra("aspectY", 1);        intent.putExtra("outputX", 500);        intent.putExtra("outputY", 500);        intent.putExtra("scale", true);        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);        intent.putExtra("return-data", false);        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());        intent.putExtra("noFaceDetection", true); // no face detection        intent = Intent.createChooser(intent, "裁剪图片");        startActivityForResult(intent, requestCode);    }
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

完全掌握 Android 选择图片、拍照以及图片裁剪

参考:
Android——调用系统相册

超完整!Android获取图片的三种方法

Intent 和 Intent 过滤器

Intent

MediaStore.Images.Media

           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

猜你喜欢

转载自blog.csdn.net/qq_43667831/article/details/87866303