先獻(xiàn)上官方連接 https://developer.android.com/training/camera/photobasics
不得不說官方文檔還是值得讀的 每次讀都會有新的收獲
最近打算寫博客 是因?yàn)橛X得紙上得來終覺淺 絕知此事要躬行 寫下來 理解的更深入 更重要的原因是 交了女朋友 想讓自己更成熟穩(wěn)重點(diǎn) 做事不能犀利弧度 總要做出個樣子出來 哈哈?? 可我是一個沒有什么毅力的人 不要太相信我 我也不知道能堅(jiān)持多久
首先構(gòu)造intent
static final int REQUEST_IMAGE_CAPTURE = 1;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
增加一層if判斷 以防NotFoundException
如果僅僅只需要縮略圖
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
}
這樣就可以滿足
但是如果需要獲取完整的圖片信息 需要傳遞給intent一個uri的地址 相當(dāng)于共享文件 android7.0以后不允許對外拋出file類型的uri 需要做一層轉(zhuǎn)換 因?yàn)檎{(diào)用系統(tǒng)照相機(jī) 是兩個app的交互過程
Uri photoURI = FileProvider.getUriForFile(this "com.example.android.fileprovider",photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
通過fileprovider 做file ->content 協(xié)議的轉(zhuǎn)換
這樣當(dāng)成功返回的時候 上面uri的地址存的就是圖片信息
關(guān)于fileprovider的使用
- Defining a FileProvider
- Specifying Available Files
- Retrieving the Content URI for a File
- Granting Temporary Permissions to a URI
-
Serving a Content URI to Another App
參考地址
https://developer.android.com/training/camera/photobasics
https://developer.android.com/reference/android/support/v4/content/FileProvider.html
http://www.reibang.com/p/55eae30d133c
https://blog.csdn.net/lmj623565791/article/details/72859156