寫文章的目的
- 靜下心來學(xué)習(xí)
- 知識點的積累
- 總結(jié)似嗤,做筆記
導(dǎo)讀
解決上一篇文章留下的問題使用Android系統(tǒng)相機捕獲圖片(二)。
需求
- 在高版本手機上淡诗,使用Android系統(tǒng)相機捕獲圖片(大圖)旱物。
問題
- 在Android6.0以上需要動態(tài)的申請
讀寫權(quán)限
:READ_EXTERNAL_STORAGE
和WRITE_EXTERNAL_STORAGE
。
2.直接將上一篇的項目運行在Android7.0的手機上會直接報錯:FileUriExposedException: file:///storage/emulated/0/rflashmy.jpg exposed beyond app through ClipData.Item.getUri()
侥祭。
代碼解構(gòu)
1.動態(tài)申請權(quán)限。
- 步驟1:在
AndroidManifest.xml
中注冊讀寫權(quán)限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- 步驟2:判斷讀寫權(quán)限是否申請。
/**
* 檢查權(quán)限是否申請
*
* @param permission 權(quán)限
* @return true :權(quán)限已申請
*/
private boolean checkPermission(String permission) {
//是否申請權(quán)限
boolean hasPermission = false;
//通過api去校驗權(quán)限是否申請卑硫,返回判斷標(biāo)志
int i = ContextCompat.checkSelfPermission(this, permission);
if (PackageManager.PERMISSION_GRANTED == i) {
//PERMISSION_GRANTED表示權(quán)限已申請
hasPermission = true;
} else if (PackageManager.PERMISSION_DENIED == i) {
//PERMISSION_DENIED表示權(quán)限未申請
hasPermission = false;
}
return hasPermission;
}
- 步驟3:申請權(quán)限。
/**
* 請求權(quán)限
*/
private void requestPermission(String... permission) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission[0])) {
//shouldShowRequestPermissionRationale()方法解釋
//1.第一次請求該權(quán)限蚕断,返回false
//2.請求過該權(quán)限并被用戶拒絕欢伏,返回true
//3.請求過該權(quán)限,但用戶拒絕的時候勾選不再提醒亿乳,返回false硝拧。
//總結(jié)一下,這個方法的目的:就是說去告訴用戶葛假,為什么需要用戶同意該權(quán)限障陶。
//todo 所以這里可以給一個ui提示用戶同意權(quán)限的好處。
//我這里只是toast聊训”Ь浚可以理解為偽代碼,只是提供一種思路
Toast.makeText(this, "求求你授權(quán)吧带斑!", Toast.LENGTH_SHORT).show();
//提示之后可以繼續(xù)請求權(quán)限
ActivityCompat.requestPermissions(this, permission, PERMISSION_RESULT);
} else {
//沒有權(quán)限鼓寺,去請求權(quán)限
ActivityCompat.requestPermissions(this, permission, PERMISSION_RESULT);
}
}
- 步驟4:回調(diào)結(jié)果中處理權(quán)限。
/**
* 讀寫權(quán)限
*/
private final String mPermission = Manifest.permission.WRITE_EXTERNAL_STORAGE;
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode != PERMISSION_RESULT) {
return;
}
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//因為我就申請一個權(quán)限勋磕,所以可以通過grantResults[0]就是我申請的權(quán)限妈候。
//用戶同意權(quán)限,打開相機
openCameraForResult();
}else {
//這里本意并不是重新請求權(quán)限挂滓,是通過方法shouldShowRequestPermissionRationale()去給用戶做提示苦银。
requestPermission(mPermission);
}
}
2.檢驗權(quán)限代碼是否能獲取權(quán)限。在 button
點擊事件里面調(diào)用獲取權(quán)限方法赶站。會發(fā)現(xiàn)點擊完全沒反應(yīng)(手機:一加5T幔虏,系統(tǒng)版本7.1)。是代碼有問題嗎贝椿?之后我換成ACCESS_COARSE_LOCATION
所计,發(fā)現(xiàn)彈出了需要用戶授權(quán)的dialog。what the fuck?
3.也就是說代碼沒問題团秽,之后我換了小米手機主胧,發(fā)現(xiàn)是需要獲取權(quán)限的。
4.這下好了權(quán)限也解決了习勤,接下來就是解決
Android 7.0
上報錯FileUriExposedException
踪栋。這是因為Android 7.0
以后不能識別以file://
開頭的Uri。也就是說在不同版本需要構(gòu)建不同的Uri图毕。
-
步驟一:
res
目錄下創(chuàng)建xml
資源文件夾(如果有不必建)夷都,然后創(chuàng)建一個xml文件(以file.xml為例)。
image.png 步驟二:完成
files.xml
予颤。還記得我們需要把拍照圖片存在哪嗎囤官?Environment.getExternalStorageDirectory()+"/rflash"+"/my.jpg"
冬阳。那么對應(yīng)的xml文件:
<!--external-path 對應(yīng)的是Environment.getExternalStorageDirectory()-->
<!--name 我的理解是Uri上的名字-->
<!--path 就是圖片存儲的真實路徑-->
<!--可以通過生成的Uri去理解這下值-->
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="image"
path="rflash" />
</paths>
- 步驟三:在
AndroidManifest.xml
的application
標(biāo)簽里面添加provider
標(biāo)簽。
<!--authorities 對應(yīng)值不一定非是applicationId-->
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.rflash.captrueimage03"
android:exported="false"
android:grantUriPermissions="true">
<!--resource 對應(yīng)值是剛剛構(gòu)建的files.xml-->
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/files" />
</provider>
- 步驟四:java代碼中構(gòu)建Uri党饮。
/**
* 獲取圖片uri
*
* @return
*/
private Uri getImageUri() {
File file = new File(mImageFilePath);
//###下面代碼有些手機需要(如:小米手機)###
//可以試下去掉下面的代碼在小米手機上報什么錯
File parentFile = file.getParentFile();
//去創(chuàng)建圖片存放的父路徑
if (!parentFile.exists()){
parentFile.mkdirs();
}
//###上面面代碼有些手機需要(如:小米手機)###
Uri imageUri;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N) {
//N以前
imageUri = Uri.fromFile(file);
} else {
//N以后肝陪,通過FileProvider生成
//特別地,第二個參數(shù)authority對應(yīng)AndroidManifest.xml下provider標(biāo)簽里面的authorities值刑顺。
//我直接使用的application_id
imageUri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID, file);
Log.i("---", imageUri.toString());
//打印結(jié)果
//I/---: content://com.rflash.captrueimage03/image/my.jpg
}
return imageUri;
}
5.小結(jié)下氯窍,權(quán)限問題
和7.0上Uri問題
都已經(jīng)解決,接下來調(diào)用系統(tǒng)相機
蹲堂。直接上代碼(與上一篇調(diào)用是一樣的)狼讨。
/**
* 打開相機
*/
private void openCameraForResult() {
//創(chuàng)建intent ,設(shè)置action
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//構(gòu)建圖片路徑
createImageFilePath();
//將捕獲的圖片保存在imageUri
intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri());
//調(diào)用相機
startActivityForResult(intent, IMAGE_RESULT);
}
6.拿到圖片并顯示(顯示結(jié)果就不截圖了)柒竞。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) {
return;
}
if (requestCode == IMAGE_RESULT) {
//這里不能通過Intent對象去獲取"data"政供,
// 因為在打開相機時已經(jīng)通過MediaStore.EXTRA_OUTPUT告訴相機:你把圖片放在我傳給你的Uri中
//所以可以直接通過BitmapFactory在存儲路徑中獲取圖片
Bitmap bitmap = BitmapFactory.decodeFile(mImageFilePath);
imageView.setImageBitmap(bitmap);
}
}
總結(jié)
1.調(diào)用系統(tǒng)相機捕獲圖片,需要注意:不同版本需要構(gòu)建不同的Uri
朽基,具體區(qū)分是Android.N
鲫骗。在N
以前可以直接使用Uri.fromFile(file)
創(chuàng)建Uri
,在N
以后需要通過FileProvider
踩晶,具體步驟文章上面有执泰。
2.特別注意:有些手機需要手動給圖片創(chuàng)建父路徑。
3.那么渡蜻,獲取了這么大的圖片并顯示术吝,內(nèi)存肯定消耗很大,有什么優(yōu)化嗎茸苇?請聽下回分解排苍。
代碼樣例
1.res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="capture_image"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn" />
</android.support.constraint.ConstraintLayout>
2.MainActivity.java
package com.rflash.captrueimage03;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button button;
private ImageView imageView;
/**
* 打開系統(tǒng)相機的requestCode
*/
private final int IMAGE_RESULT = 0;
/**
* 請求權(quán)限的requestCode
*/
private final int PERMISSION_RESULT = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.btn);
imageView = findViewById(R.id.iv);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.btn:
clickButton();
break;
}
}
/**
* 點擊 capture_image button
*/
private void clickButton() {
if (!checkPermission(mPermission)) {
requestPermission(mPermission);
} else {
//打開相機
openCameraForResult();
}
}
/**
* 讀寫權(quán)限
*/
private final String mPermission = Manifest.permission.WRITE_EXTERNAL_STORAGE;
/**
* 圖片存儲路徑
*/
private String mImageFilePath;
/**
* 構(gòu)建圖片路徑
* 將圖片保存在Environment.getExternalStorageDirectory()+"/rflash"+"/my.jpg"
*/
private void createImageFilePath() {
mImageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "rflash" + File.separator + "my.jpg";
}
/**
* 獲取圖片uri
*
* @return
*/
private Uri getImageUri() {
File file = new File(mImageFilePath);
//###下面代碼有些手機需要(如:小米手機)###
//可以試下去掉下面的代碼在小米手機上報什么錯
File parentFile = file.getParentFile();
//去創(chuàng)建圖片存放的父路徑
if (!parentFile.exists()){
parentFile.mkdirs();
}
//###上面面代碼有些手機需要(如:小米手機)###
Uri imageUri;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N) {
//N以前
imageUri = Uri.fromFile(file);
} else {
//N以后,通過FileProvider生成
//特別地学密,第二個參數(shù)authority對應(yīng)AndroidManifest.xml下provider標(biāo)簽里面的authorities值淘衙。
//我直接使用的application_id
imageUri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID, file);
Log.i("---", imageUri.toString());
//打印結(jié)果
//I/---: content://com.rflash.captrueimage03/image/my.jpg
}
return imageUri;
}
/**
* 檢查權(quán)限是否申請
*
* @param permission 權(quán)限
* @return true :權(quán)限已申請
*/
private boolean checkPermission(String permission) {
//是否申請權(quán)限
boolean hasPermission = false;
//通過api去校驗權(quán)限是否申請,返回判斷標(biāo)志
int i = ContextCompat.checkSelfPermission(this, permission);
if (PackageManager.PERMISSION_GRANTED == i) {
//PERMISSION_GRANTED表示權(quán)限已申請
hasPermission = true;
} else if (PackageManager.PERMISSION_DENIED == i) {
//PERMISSION_DENIED表示權(quán)限未申請
hasPermission = false;
}
return hasPermission;
}
/**
* 請求權(quán)限
*/
private void requestPermission(String... permission) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission[0])) {
//shouldShowRequestPermissionRationale()方法解釋
//1.第一次請求該權(quán)限腻暮,返回false
//2.請求過該權(quán)限并被用戶拒絕彤守,返回true
//3.請求過該權(quán)限,但用戶拒絕的時候勾選不再提醒哭靖,返回false具垫。
//總結(jié)一下,這個方法的目的:就是說去告訴用戶试幽,為什么需要用戶同意該權(quán)限筝蚕。
//todo 所以這里可以給一個ui提示用戶同意權(quán)限的好處。
//我這里只是toast∑鹂恚可以理解為偽代碼洲胖,只是提供一種思路
Toast.makeText(this, "求求你授權(quán)吧!", Toast.LENGTH_SHORT).show();
//提示之后可以繼續(xù)請求權(quán)限
ActivityCompat.requestPermissions(this, permission, PERMISSION_RESULT);
} else {
//沒有權(quán)限坯沪,去請求權(quán)限
ActivityCompat.requestPermissions(this, permission, PERMISSION_RESULT);
}
}
/**
* 打開相機
*/
private void openCameraForResult() {
//創(chuàng)建intent 绿映,設(shè)置action
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//構(gòu)建圖片路徑
createImageFilePath();
//將捕獲的圖片保存在imageUri
intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri());
//調(diào)用相機
startActivityForResult(intent, IMAGE_RESULT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) {
return;
}
if (requestCode == IMAGE_RESULT) {
//這里不能通過Intent對象去獲取"data",
// 因為在打開相機時已經(jīng)通過MediaStore.EXTRA_OUTPUT告訴相機:你把圖片放在我傳給你的Uri中
//所以可以直接通過BitmapFactory在存儲路徑中獲取圖片
Bitmap bitmap = BitmapFactory.decodeFile(mImageFilePath);
imageView.setImageBitmap(bitmap);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode != PERMISSION_RESULT) {
return;
}
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//因為我就申請一個權(quán)限屏箍,所以可以通過grantResults[0]就是我申請的權(quán)限。
//用戶同意權(quán)限橘忱,打開相機
openCameraForResult();
} else {
//這里本意并不是重新請求權(quán)限赴魁,是通過方法shouldShowRequestPermissionRationale()去給用戶做提示。
requestPermission(mPermission);
}
}
}
3.res/xml/files.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<!--external-path 對應(yīng)的是Environment.getExternalStorageDirectory()-->
<!--name 我的理解是Uri上的名字-->
<!--path 就是圖片存儲的真實路徑-->
<!--可以通過生成的Uri去理解這下值-->
<external-path
name="image"
path="rflash" />
</paths>
4.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rflash.captrueimage03">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--authorities 對應(yīng)值不一定非是applicationId-->
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.rflash.captrueimage03"
android:exported="false"
android:grantUriPermissions="true">
<!--resource 對應(yīng)值是剛剛構(gòu)建的files.xml-->
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/files" />
</provider>
</application>
</manifest>