目錄
MainActivity 代碼修改
這一部分的代碼,問題出在onActivityResult這個方法上,這裏會要求Overriding method should call super.onActivityResult,這裏需要在@Override上添加一行註釋
@SuppressLint("MissingSuperCall")
完整代碼:
@SuppressLint("MissingSuperCall")
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case TAKE_PHOTO:
if (resultCode == RESULT_OK) {
try{
//將拍攝的圖片顯示出來
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
picture.setImageBitmap(bitmap);
} catch (FileNotFoundException e){
e.printStackTrace();
}
}
break;
default:
break;
}
}
AndroidManifest.xml
在AndroidManifest.xml文件中的<provider>標籤中android:name="android.support.v4.content.FileProvider"修改爲
android:name="androidx.core.content.FileProvider"
完整代碼:
<provider
android:authorities="com.example.cameraalbumtest.fileprovider"
android:name="androidx.core.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
build.gradle
程序運行報錯:
Installation did not succeed.
The application could not be installed: INSTALL_FAILED_OLDER_SDK
The application's minSdkVersion is newer than the device API level.
修改build.gradle
修改前:
修改後:
file_paths.xml
這裏path=""會報錯,不需要理睬。
總結
完成上述修改即可運行書上的實例,歡迎交流。