Camera Intent 不会将图像保存到图库
Camera Intent doesn't save image to gallery
我是新手android developer.Now我在编译相机应用程序时发现错误。
实际上相机意图工作正常,拍摄的图像可以显示在 ImageView 上,但它不会自动保存到我的 phone 图库中。经过研究,照片保存到图库需要 3 天。
我已经尝试了一些不同的相机意图方法,但直到现在我还没有明显的结果。非常感谢任何帮助,谢谢。
我的 target SDK is 27 API
,我已经为 provider_paths
制作了一个 XML 文件并在我的 manifests
.
中描述了它
这是我的相机意图:
public static final int REQUEST_CAMERA = 100;
Uri fileUri;
public final int SELECT_FILE = 11;
int bitmap_size = 40; // image quality 1 - 100;
int max_resolution_image = 800;
private void openCamera() {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri();
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, REQUEST_CAMERA);
}
public Uri getOutputMediaFileUri() {
return FileProvider.getUriForFile(LaporActivity.this,
BuildConfig.APPLICATION_ID + ".fileprovider",
getOutputMediaFile());
}
private static File getOutputMediaFile() {
// External sdcard location
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "KSD");
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.e("Monitoring", "Oops! Failed create Monitoring directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_KSD_" + timeStamp + ".jpg");
return mediaFile;
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.e("onActivityResult", "requestCode " + requestCode + ", resultCode " + resultCode);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
try {
Log.e("CAMERA", fileUri.getPath());
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(fileUri));
setToImageView(getResizedBitmap(bitmap, max_resolution_image));
} catch (Exception e) {
e.printStackTrace();
}
} else if (requestCode == SELECT_FILE && data != null && data.getData() != null) {
try {
// choose picture from Gallery
bitmap = MediaStore.Images.Media.getBitmap(LaporActivity.this.getContentResolver(), data.getData());
setToImageView(getResizedBitmap(bitmap, max_resolution_image));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private void setToImageView(Bitmap bmp) {
//compress image
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, bitmap_size, bytes);
decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(bytes.toByteArray()));
//shows chosen picture from gallery to imageview
fotoCaptured.setImageBitmap(decoded);
}
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);
}
我希望图片会自动保存到我的画廊,这样我就可以继续使用 Retrofit 上传处理方法,但实际情况是当我点击上传按钮时,toast 显示 "IMGxxxxx No such file/directory" 因为图片路径未正确保存。
您的文件存在于内存中,但不存在于磁盘中。返回对象前创建文件
private static File getOutputMediaFile() {
...
...
//create file on disk
if(!mediaFile.exists()) mediaFile.createNewFile();
return mediaFile;
}
我是新手android developer.Now我在编译相机应用程序时发现错误。
实际上相机意图工作正常,拍摄的图像可以显示在 ImageView 上,但它不会自动保存到我的 phone 图库中。经过研究,照片保存到图库需要 3 天。
我已经尝试了一些不同的相机意图方法,但直到现在我还没有明显的结果。非常感谢任何帮助,谢谢。
我的 target SDK is 27 API
,我已经为 provider_paths
制作了一个 XML 文件并在我的 manifests
.
这是我的相机意图:
public static final int REQUEST_CAMERA = 100;
Uri fileUri;
public final int SELECT_FILE = 11;
int bitmap_size = 40; // image quality 1 - 100;
int max_resolution_image = 800;
private void openCamera() {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri();
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, REQUEST_CAMERA);
}
public Uri getOutputMediaFileUri() {
return FileProvider.getUriForFile(LaporActivity.this,
BuildConfig.APPLICATION_ID + ".fileprovider",
getOutputMediaFile());
}
private static File getOutputMediaFile() {
// External sdcard location
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "KSD");
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.e("Monitoring", "Oops! Failed create Monitoring directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_KSD_" + timeStamp + ".jpg");
return mediaFile;
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.e("onActivityResult", "requestCode " + requestCode + ", resultCode " + resultCode);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
try {
Log.e("CAMERA", fileUri.getPath());
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(fileUri));
setToImageView(getResizedBitmap(bitmap, max_resolution_image));
} catch (Exception e) {
e.printStackTrace();
}
} else if (requestCode == SELECT_FILE && data != null && data.getData() != null) {
try {
// choose picture from Gallery
bitmap = MediaStore.Images.Media.getBitmap(LaporActivity.this.getContentResolver(), data.getData());
setToImageView(getResizedBitmap(bitmap, max_resolution_image));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private void setToImageView(Bitmap bmp) {
//compress image
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, bitmap_size, bytes);
decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(bytes.toByteArray()));
//shows chosen picture from gallery to imageview
fotoCaptured.setImageBitmap(decoded);
}
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);
}
我希望图片会自动保存到我的画廊,这样我就可以继续使用 Retrofit 上传处理方法,但实际情况是当我点击上传按钮时,toast 显示 "IMGxxxxx No such file/directory" 因为图片路径未正确保存。
您的文件存在于内存中,但不存在于磁盘中。返回对象前创建文件
private static File getOutputMediaFile() {
...
...
//create file on disk
if(!mediaFile.exists()) mediaFile.createNewFile();
return mediaFile;
}