16.1
1.添加实例变量(CrimeFragment.java)
private ImageButton mPhotoButton;
private ImageView mPhotoView;
mPhotoButton = (ImageButton) v.findViewById(R.id.crime_camera);
mPhotoView = (ImageView) v.findViewById(R.id.crime_photo);
16.2
16.2.1
1.添加FileProvider声明(Android Manifest.xml)
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.bignerdranch.android.criminalintent.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
</provider>
2.创建xml/files.xml文件
3.填写路径描述(res/xml/files.xml)
打开刚建的files文件
删掉原内容,加上
<paths>
<files-path name="crime_photos" path="."/>
</paths>
4.关联使用路径描述资源(AndroidManifest.xml)
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/files" />
16.2.2
1.添加文件名获取方法(Crime.java)
public String getPhotoFilename() {
return "IMG_" + getId().toString() + ".jpg";
}
2.定位照片文件(CrimeLab.java)
public File getPhotoFile(Crime crime) {
File filesDir = mContext.getFilesDir();
return new File(filesDir, crime.getPhotoFilename());
}
16.3
1.获取照片文件位置(CrimeFragment.java)
private File mPhotoFile;
mPhotoFile = CrimeLab.get(getActivity()).getPhotoFile(mCrime);
2.使用相机intent(CrimeFragment.java)
private static final int REQUEST_PHOTO = 2;
final Intent captureImage = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
boolean canTakePhoto = mPhotoFile != null &&
captureImage.resolveActivity(packageManager) != null;
mPhotoButton.setEnabled(canTakePhoto);
mPhotoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = FileProvider.getUriForFile(getActivity(),
"com.bignerdranch.android.criminalintent.fileprovider",
mPhotoFile);
captureImage.putExtra(MediaStore.EXTRA_OUTPUT, uri);
List<ResolveInfo> cameraActivities = getActivity()
.getPackageManager().queryIntentActivities(captureImage,
PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo activity : cameraActivities) {
getActivity().grantUriPermission(activity.activityInfo.packageName,
uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
startActivityForResult(captureImage, REQUEST_PHOTO);
}
});
16.4
1.创建名为PictureUtils的新类,并添加
getScaledBitmap(…)方法(PictureUtils.java)
public static Bitmap getScaledBitmap(String path, int destWidth, int destHeight) {
// Read in the dimensions of the image on disk
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
float srcWidth = options.outWidth;
float srcHeight = options.outHeight;
// Figure out how much to scale down by
int inSampleSize = 1;
if (srcHeight > destHeight || srcWidth > destWidth) {
float heightScale = srcHeight / destHeight;
float widthScale = srcWidth / destWidth;
inSampleSize = Math.round(heightScale > widthScale ? heightScale :
widthScale);
}
options = new BitmapFactory.Options();
options.inSampleSize = inSampleSize;
// Read in and create final bitmap
return BitmapFactory.decodeFile(path, options);
}
2.编写合理的缩放方法(PictureUrils.java)
public static Bitmap getScaledBitmap(String path, Activity activity) {
Point size = new Point();
activity.getWindowManager().getDefaultDisplay()
.getSize(size);
return getScaledBitmap(path, size.x, size.y);
}
3.更新mPhotoView(CrimeFragment.java)
private void updatePhotoView() {
if (mPhotoFile == null || !mPhotoFile.exists()) {
mPhotoView.setImageDrawable(null);
} else {
Bitmap bitmap = PictureUtils.getScaledBitmap(
mPhotoFile.getPath(), getActivity());
mPhotoView.setImageBitmap(bitmap);
}
}
4.调用updatePhotoView()方法(CrimeFragment.java)
updatePhotoView();
} else if (requestCode == REQUEST_PHOTO) {
Uri uri = FileProvider.getUriForFile(getActivity(),
"com.bignerdranch.android.criminalintent.fileprovider",
mPhotoFile);
getActivity().revokeUriPermission(uri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
updatePhotoView();
16.5
1.添加标签(AndroidManifest.xml)
<uses-feature
android:name="android.hardware.camera"
android:required="false" />