Android 如何配置ACTION_IMAGE_CAPTURE将照片存储在公共外部存储器中?
建议将设备摄像头拍摄的图像存储为“应保存在设备的公共外部存储器中”。但如何做到这一点呢?提供的示例演示了如何保存到私人文件夹 提供的示例演示了如何保存到私人文件夹 不,没有。它显示了如何保存到Android 如何配置ACTION_IMAGE_CAPTURE将照片存储在公共外部存储器中?,android,android-image-capture,Android,Android Image Capture,建议将设备摄像头拍摄的图像存储为“应保存在设备的公共外部存储器中”。但如何做到这一点呢?提供的示例演示了如何保存到私人文件夹 提供的示例演示了如何保存到私人文件夹 不,没有。它显示了如何保存到getExternalFilesDir()。这是的一部分,用户可以访问 如果您愿意,还可以在环境.getExternalStoragePublicDirectory(Environment.DIRECTORY\u DCIM)下创建一个子目录。不过,您需要在所有API级别上使用WRITE\u EXTERNAL
getExternalFilesDir()
。这是的一部分,用户可以访问
如果您愿意,还可以在环境.getExternalStoragePublicDirectory(Environment.DIRECTORY\u DCIM)
下创建一个子目录。不过,您需要在所有API级别上使用WRITE\u EXTERNAL\u STORAGE
,包括需要在Android 6.0+上设置运行时权限
提供的示例演示了如何保存到私人文件夹
不,没有。它显示了如何保存到getExternalFilesDir()
。这是的一部分,用户可以访问
如果您愿意,还可以在
环境.getExternalStoragePublicDirectory(Environment.DIRECTORY\u DCIM)
下创建一个子目录。不过,您需要在所有API级别上编写外部存储,包括需要在Android 6.0+上设置运行时权限。查看此代码它肯定会帮助您:
私有void selectImage(){
请参阅此代码,它肯定会帮助您: 私有void selectImage(){ 试试这个
//Click Picture from camera
camera_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New Picture");
values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CAMERA_CLICK_RESULT);
}
});
结果是这样的
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Get Image from Camera
if (requestCode == CAMERA_CLICK_RESULT && resultCode == RESULT_OK) {
Bitmap photo = null;
try {
photo = MediaStore.Images.Media.getBitmap(
getContentResolver(), imageUri);
} catch (IOException e) {
e.printStackTrace();
}
selectedImage = getResizedBitmap(photo, 900);
try {
//Write file
String filename = "/file_name";
String dir_path = "Directory_Path";
File file = new File(dir_path)
file.mkdir();
FileOutputStream fileOutputStream = new FileOutputStream(dir_path + filename);
selectedImage.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
//Cleanup
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//Resize Bitmap
public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
int width = image.getWidth();
int height = image.getHeight();
float bitmapRatio = (float) width / (float) height;
if (bitmapRatio > 1) {
width = maxSize;
height = (int) (width / bitmapRatio);
} else {
height = maxSize;
width = (int) (height * bitmapRatio);
}
return Bitmap.createScaledBitmap(image, width, height, true);
}
试试这个
//Click Picture from camera
camera_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New Picture");
values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CAMERA_CLICK_RESULT);
}
});
结果是这样的
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Get Image from Camera
if (requestCode == CAMERA_CLICK_RESULT && resultCode == RESULT_OK) {
Bitmap photo = null;
try {
photo = MediaStore.Images.Media.getBitmap(
getContentResolver(), imageUri);
} catch (IOException e) {
e.printStackTrace();
}
selectedImage = getResizedBitmap(photo, 900);
try {
//Write file
String filename = "/file_name";
String dir_path = "Directory_Path";
File file = new File(dir_path)
file.mkdir();
FileOutputStream fileOutputStream = new FileOutputStream(dir_path + filename);
selectedImage.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
//Cleanup
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//Resize Bitmap
public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
int width = image.getWidth();
int height = image.getHeight();
float bitmapRatio = (float) width / (float) height;
if (bitmapRatio > 1) {
width = maxSize;
height = (int) (width / bitmapRatio);
} else {
height = maxSize;
width = (int) (height * bitmapRatio);
}
return Bitmap.createScaledBitmap(image, width, height, true);
}
动作图像捕获方法:
// OPEN CAMERA & TAKE PIC
private void dispatchTakePictureIntent() {
try {
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) {
// Error occurred while creating the File
ex.printStackTrace();
Log.e("takePic_IO_EX", ex + "");
}
//photoFile = createImageFile();
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.appname.fileprovider",
photoFile);
List<ResolveInfo> resInfoList = this.getPackageManager().queryIntentActivities(takePictureIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
this.grantUriPermission(packageName, photoURI, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
/*takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
this.setResult(RESULT_OK, takePictureIntent);*/
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
} catch (Exception e) {
e.printStackTrace();
Log.e("takePic_main_EX", e + "");
}
}
并在res
文件夹中创建一个名为xml
的文件夹。在该xml
文件夹中,创建一个xml文件
并将其命名为file\u path.xml
。在文件路径.xml中提及此路径
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="Images" path="Android/data/" />
</paths>
全局声明文件photoFile
。
祝您一切顺利!动作图像捕获方法:
// OPEN CAMERA & TAKE PIC
private void dispatchTakePictureIntent() {
try {
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) {
// Error occurred while creating the File
ex.printStackTrace();
Log.e("takePic_IO_EX", ex + "");
}
//photoFile = createImageFile();
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.appname.fileprovider",
photoFile);
List<ResolveInfo> resInfoList = this.getPackageManager().queryIntentActivities(takePictureIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
this.grantUriPermission(packageName, photoURI, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
/*takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
this.setResult(RESULT_OK, takePictureIntent);*/
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
} catch (Exception e) {
e.printStackTrace();
Log.e("takePic_main_EX", e + "");
}
}
并在res
文件夹中创建一个名为xml
的文件夹。在该xml
文件夹中,创建一个xml文件
并将其命名为file\u path.xml
。在文件路径.xml中提及此路径
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="Images" path="Android/data/" />
</paths>
全局声明文件photoFile
。
祝您一切顺利!虽然保留文件对象后,我不需要在onActivityResult.ya中搜索它,因为您知道代码的流程。尽管保留文件对象后,我不需要在onActivityResult.ya中搜索它,因为您知道代码的流程。这同样有效,但不是使用此方法可以获取旋转信息。此方法也有效,但无法使用此方法获取旋转信息。