我正在尝试拍照并保存在Android中。我已阅读本教程take photo simple android training
在教程中,我看到我们通过
获得了UriFileProvider.getUriForFile()
Uri photoURI = FileProvider.getUriForFile(this,"com.example.android.fileprovider",photoFile);
但是当我点击按钮下载示例并从那里下载演示时,此示例中的代码通过
创建UriUri.fromFile()
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
我已经看到示例的compileSdkVersion是25但我的项目使用
compileSdkVersion 26
所以,如果我使用Uri.fromFile(),我的项目会出错(如果我们将项目26的compileSdkVersion更改为25,则没有错误,但我认为我不应该这样做)
所以我的问题是如何使用FileProvider和compileSdkVersion> = 26拍摄照片。
答案 0 :(得分:1)
在Android中获取和存储图片的一种好方法是使用EasyImage库。 EasyImage允许您轻松地从图库,相机或文档中捕获图像,而无需创建大量样板。
查看EasyImage android链接。
基本用法如下,提前使用请参考上面的链接。
直接打开相机:
EasyImage.openCamera(Fragment fragment, int type);
在 onActivityResult()中,您可以再次使用EasyImage并使用它的 handleActivityResult()方法来覆盖它的两个方法 onImagePickerError()和 onImagesPicked()做相关工作。
答案 1 :(得分:0)
我创建了一个使用Content provider和compileSdkVersion 26拍摄照片的示例。我将我的代码放在这里以供任何人使用
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
ex.printStackTrace();
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(
this,
"com.funny.fileprovider",
photoFile);
Log.d("=DEBUG=",photoURI.toString());
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
NewNoteActivity.this.grantUriPermission("com.example.funny.stickynote",photoURI, FLAG_GRANT_READ_URI_PERMISSION);
NewNoteActivity.this.grantUriPermission("com.example.funny.stickynote",photoURI, FLAG_GRANT_WRITE_URI_PERMISSION);
takePictureIntent.setFlags(FLAG_GRANT_READ_URI_PERMISSION);
takePictureIntent.setFlags(FLAG_GRANT_WRITE_URI_PERMISSION);
//takePictureIntent.setData(photoURI);
NewNoteActivity.this.setResult(RESULT_OK, takePictureIntent);
PackageManager packageManager = getPackageManager();
//if (takePictureIntent.resolveActivity(packageManager) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
//}
}
}
}
收到结果时
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(moreOptionDialog.isShowing()==true)
{
moreOptionDialog.dismiss();
}
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)
{
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap imageBitmap = BitmapFactory.decodeFile(currentPhotoPath, bmOptions);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
(int)getResources().getDimension(R.dimen.image_note_size),
(int)getResources().getDimension(R.dimen.image_note_size)
);
lp.setMargins(
(int)getResources().getDimension(R.dimen.image_note_margin),
(int)getResources().getDimension(R.dimen.image_note_margin),
(int)getResources().getDimension(R.dimen.image_note_margin),
(int)getResources().getDimension(R.dimen.image_note_margin)
);
// Do other work with full size photo saved in mLocationForPhotos
ImageView imageView = new ImageView(this);
//imageView.setBackgroundColor(ContextCompat.getColor(NewNoteActivity.this, R.color.blue));
imageView.setLayoutParams(
new ViewGroup.LayoutParams(lp)
);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setImageBitmap(imageBitmap);
imageListAreaLinearLayout.addView(imageView, lp);
}
}
manifest.xml中的
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.funny.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
file_paths
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="pic" path="Pictures" />
</paths>