前言
源码Demo:请点击此处
Android调用系统相机会遇到的两大问题:
- 1.指定存储图片路径,Android7.0及之后的机型调用系统相机会抛出android.os.FileUriExposedException异常
- 2.指定存储图片路径,调用系统相机返回 intent 为:null
问题《一》
- Android 7.0后系统禁止应用向外部公开file://URI ,因此需要FileProvider来向外界传递URI。所以针对安卓7.0及其之后的系统需要做一个适配。
- 实际开发中,推荐该方式。知道文件路径,可以根据需求执行相应压缩处理。
开始代码示例(Android Studio, SdkVersion 29)
- 1️⃣AndroidManifest.xml 清单文件中添加所需权限
<!--相机权限-->
<uses-permission android:name="android.permission.CAMERA" />
<!--SD卡权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- 2️⃣ activity_play_photo(PlayPhotoActivity的xml界面)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:orientation="vertical">
<ImageView
android:id="@+id/ivMyPhoto"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<Button
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:gravity="center"
android:onClick="playPhoto"
android:padding="16dp"
android:text="拍照(原图-路径获取)"
android:textColor="#FF212121"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
- 3️⃣ PlayPhotoActivity(activity中调用相机拍照并返回展示图片)
public class PlayPhotoActivity extends BaseActivity {
//定义一个文件夹路径
private String localPath = MyApplication.localPath + File.separator + "123";
private ImageView ivMyPhoto;
private File photoFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_photo);
ivMyPhoto = findViewById(R.id.ivMyPhoto);
photoFile = new File(localPath, "temp.png");
if ((photoFile.getParentFile() != null) && (!photoFile.getParentFile().exists())) {
photoFile.getParentFile().mkdirs();
}
Log.e("相机", "路径-localPath:" + localPath);
}
//相机点击事件:打开照相机(该方式获取到的图片是原图)
public void playPhoto(View view) {
//创建打开本地相机的意图对象
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//设置图片的保存位置(兼容Android7.0)
Uri fileUri = getUriForFile(this, photoFile);
//指定图片保存位置
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
//开启意图
startActivityForResult(intent, 100);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
//拍照完成后返回调用
if (resultCode == RESULT_OK) {
if (requestCode == 100) {
//该方式获取到的图片是原图
FileInputStream fis = null;
try {
fis = new FileInputStream(photoFile);
Bitmap bitmap = BitmapFactory.decodeStream(fis);
ivMyPhoto.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
private Uri getUriForFile(Context context, File file) {
Uri fileUri;
if (Build.VERSION.SDK_INT >= 24) {
//参数:authority 需要和清单文件中配置的保持完全一致:${applicationId}.xxx
fileUri = FileProvider.getUriForFile(context, context.getPackageName() + ".xxx", file);
} else {
fileUri = Uri.fromFile(file);
}
return fileUri;
}
}
- 4️⃣ 清单文件配置
- SdkVersion 29之前使用:android.support.v4(下述)
android:name="android.support.v4.content.FileProvider" - SdkVersion 29开始使用:androidx(下述)
android:name="androidx.core.content.FileProvider" - authorities可以随意定义(默认规程:采用本应用包名+定义串)
android:authorities="包名.xxx"
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.xxx"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
- 5️⃣在 res 目录下创建 xml 目录,并在res/xml目录下创建文件:file_paths(代码如示)
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<root-path
name="root"
path="" />
<!--files-path 相当于 getFilesDir()-->
<files-path
name="files"
path="path" />
<!--cache-path 相当于 getCacheDir()-->
<cache-path
name="cache"
path="path" />
<!--external-path 相当于 Environment.getExternalStorageDirectory()-->
<external-path
name="external"
path="path" />
<!--external-files-path 相当于 getExternalFilesDir("") -->
<external-files-path
name="external-files"
path="path" />
<!--external-cache-path 相当于 getExternalCacheDir() -->
<external-cache-path
name="external-cache"
path="path" />
</paths>
问题《二》
- 在调用系统相机的时候,如果传入了:指定的路径(文件保存地址),那么在activity的回调方法:onActivityResult 中,intent对象会是null。
- 如问题一的示例代码:onActivityResult的intent对象亦是null
- 如何解决呢?可以参考下述代码(但实际开发中,不推荐该方式,该方式获取到的图片数据是Android系统压缩后的图片。)
开始代码示例(Android Studio, SdkVersion 29)
- 1️⃣ 参考《问题一》第一步
- 2️⃣ 参考《问题一》第二步
- 3️⃣ PlayPhotoActivity(activity中调用相机拍照并返回展示图片)
public class PlayPhotoActivity extends BaseActivity {
private ImageView ivMyPhoto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_photo);
ivMyPhoto = findViewById(R.id.ivMyPhoto);
}
//相机点击事件:打开照相机(该方式获取到的图片是缩略图)
public void playPhoto(View view) {
//创建打开本地相机的意图对象
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//如果intent指定了存储图片的路径,那么onActivityResult回调中Intent对象就会为null
//intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
//开启意图
startActivityForResult(intent, 200);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
//拍照完成后返回调用
if (resultCode == RESULT_OK) {
if (requestCode == 200) {
//该方式获取到的图片是缩略图
Bundle bundle = intent.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");
ivMyPhoto.setImageBitmap(bitmap);
}
}
}
}