我想这样做,所以我的Android应用程序(最低API 18)可以拍照并将其保存到应用程序存储中的文件夹中。然后,在ImageView中显示图像。到目前为止,我已经尝试https://developer.android.com/training/camera/photobasics.html#TaskPhotoView使用setPic()和dispatchTakePictureIntent()与createImageFile(),但我收到错误" java.lang.IllegalArgumentException:无法找到包含/ storage /的已配置根目录模拟/ 0 / Android设备/数据/ com.example.app /文件/图片/ JPEG_20170503_191954_1677887271.jpg"
答案 0 :(得分:0)
将此内容放入Android Manifest
application
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.yourpackagename.yourappname.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
res
并且不要忘记在名称为file_paths.xml
的{{1}}文件夹中创建一个xml文件,并提及这样的路径
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="appImgs" path="Android/data/" />
你可以通过这样的File Provider
来打电话
Uri photoURI = FileProvider.getUriForFile(this,
"com.yourpackagename.fileprovider",
yourImageFile);
文件名yourImageFile
可能包含其中的位置。
希望这会有所帮助。
答案 1 :(得分:-1)
private void takePhoto() {
imageUri = generateTimeStampPhotoFileUri();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageUri));
startActivityForResult(intent, 2);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 2) {
try {
Glide.with(this).load(imageUri).into(image_selected);
} catch (Exception e) {
Logger.shortToast(getString(R.string.captured_image_invalid));
}
}
}
}
private File generateTimeStampPhotoFileUri() {
File outputDir = getPhotoDirectory();
if (outputDir != null) {
return new File(outputDir, System.currentTimeMillis() + ".png");
}
return null;
}
private File getPhotoDirectory() {
File outputDir = null;
String externalStorageStagte = Environment.getExternalStorageState();
if (externalStorageStagte.equals(Environment.MEDIA_MOUNTED)) {
File photoDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
outputDir = new File(photoDir, getString(R.string.app_name));
if (!outputDir.exists())
if (!outputDir.mkdirs()) {
Toast.makeText(this, "Failed to create directory " + outputDir.getAbsolutePath(), Toast.LENGTH_SHORT).show();
outputDir = null;
}
}
return outputDir;
}
答案 2 :(得分:-1)
注意:对于超过23的api,您需要传递运行时权限,如果未通过权限则意味着它会抛出异常。在23以下的api您无需通过运行时权限。
您可以使用以下代码
从图库中选择文件 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//intent.addFlags(ST)
startActivityForResult(Intent.createChooser(intent, "Choose File to Upload.."), PICK_FILE_REQUEST);
然后调用OnActivityResult()方法获取意图数据
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == PICK_FILE_REQUEST) {
if (data != null) {
//no data present
Uri uri = data.getData();
String filePath = data.getData().getPath();
String name = getContentName(getContentResolver(), uri);
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
} catch (IOException e) {
e.printStackTrace();
}
imageView.setImageBitmap(bitmap);
textView.setText(name);
return;
}
}
}
然后调用以下方法从存储中获取文件名并将图像设置为imageview
`
public static String getContentName(ContentResolver resolver, Uri uri) {
Cursor cursor = resolver.query(uri, null, null, null, null);
cursor.moveToFirst();
int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
if (nameIndex >= 0) {
return cursor.getString(nameIndex);
} else {
return null;
}
}`
请尝试这样做有效....