我是一个新手android开发者。现在我在编译我的相机应用程序时发现了一个错误。
实际上相机 Intent 工作正常,拍摄的图像可以显示在 ImageView 上,但它不会自动保存到我的手机图库中。经过研究,照片保存到图库需要 3 天。
我已经尝试了一些不同的相机 Intent 方法,但直到现在我还没有取得明显的成果。非常感谢任何帮助,谢谢。
我的目标 SDK 是 27 API
,我已经为 provider_paths
制作了一个 XML 文件,并在我的 manifests
中描述了它。
这是我的相机 Intent :
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 没有这样的文件/目录”,因为图片路径未正确保存。
最佳答案
您的文件存在于内存中但不在磁盘上。返回对象前创建文件
private static File getOutputMediaFile() {
...
...
//create file on disk
if(!mediaFile.exists()) mediaFile.createNewFile();
return mediaFile;
}
关于java - Camera Intent 不会将图像保存到图库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57301042/