android - 从我的应用程序文件夹中删除的图像仍在相机库中
问题描述
我正在尝试删除通过 Camera api 拍摄的图像,原则上它确实会从我的应用程序文件夹中删除它们,但图像仍在 DCIM/camera 文件夹中。
我正在使用具有 Android 版本 9 的 ITOS 设备。
这是我正在使用的代码。
manifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature
android:name="android.hardware.camera"
android:required="false" />
------
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.cameratest.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
@xml/file_paths
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external"
path="." />
<external-files-path
name="external_files"
path="." />
<cache-path
name="cache"
path="." />
<external-cache-path
name="external_cache"
path="." />
<files-path
name="files"
path="." />
</paths>
这就是我捕获图像的方式:
private void checkPermissions(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_CAMERA_PERMISSION_CODE);
} else {
captureImage();
}
} else {
captureImage();
}
}
private void captureImage() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getPackageManager()) != null) {
File imageFile = null;
try {
imageFile = getImageFile();
} catch (IOException e) {
Log.d("TAG", "captureImage ERROR: " + e.getMessage());
e.printStackTrace();
}
if (imageFile != null) {
Uri imageUri = FileProvider.getUriForFile(this, Constants.FILE_AUTHORITY_PROVIDER, imageFile);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
}
private File getImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "IMG_" + timeStamp;
File storageDir = new File(this.getExternalFilesDir(Environment.DIRECTORY_PICTURES),"MisFotos");
if (! storageDir.exists()){
if (! storageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
File image = new File(storageDir.getPath() + File.separator + imageFileName + ".jpg");
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = image.getAbsolutePath();
Log.d("PHOTO_TAG", "getImageFile: " + currentPhotoPath);
return image;
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
setPictureToDataBase();
}
}
更新 我将图像路径保存在数据库中,然后检索它以删除文件。
这是我用来删除图片适配器中的图像的代码
public void deletePicture(Picture picture) {
try {
myDatabase.deletePicture(picture.getUid());
String path = picture.getImage();//storageDir.getPath() + File.separator + imageFileName + ".jpg"
pictures.remove(picture);
notifyDataSetChanged();
if (pictures != null && pictures.size()>0){
listener.onDeleteClick( pictures.size());
}else {
listener.onDeleteClick( 0);
}
File target = new File(path);
if (target.exists()) {
target.delete();
}
else {
Log.d("TAG_DELETE_PICTURE", "ERROR: FILE NOT EXITS");
}
} catch (Exception e) {
Log.d("deletePicture", "ERROR: " + e.getMessage());
}
}
当我打开设备文件资源管理器时,它位于“MyPhotos”和 DCIM/相机中。 同一张图,两个地方
从应用程序文件夹中删除图像时它可以正常工作,但是在 DCIM/相机中查看时图像仍然存在并占用内存。
我做错了什么,有没有办法删除保存在DCIM/camera中的图像?
我想拍照,仅将其保存在应用程序的文件夹中,而不是将其保存在其他任何地方。有没有其他方法可以做到这一点?
解决方案
难道我做错了什么
您正在通过 启动相机应用程序ACTION_IMAGE_CAPTURE
。有数以万计的 Android 设备型号。这些设备型号将预装数十种不同的相机应用程序,用户可以从 Play 商店和其他地方安装其他相机应用程序。
这些相机应用程序做什么取决于他们的开发人员。
在您的情况下,您碰巧使用的相机应用程序既将照片保存在其正常位置,又在EXTRA_OUTPUT
. 很少有相机应用程序会这样做,但相机应用程序以这种方式运行是完全合法的。
我想拍照,仅将其保存在应用程序的文件夹中,而不是将其保存在其他任何地方。有没有其他方法可以做到这一点?
不要使用ACTION_IMAGE_CAPTURE
. 相反,直接或通过包装库(Google 的 CameraX、FotoApparat、CameraKit-Android 等)使用相机 API。
推荐阅读
- google-cloud-platform - 如何在 Google Cloud ML Engine 上的 config.yaml 文件中添加用户特定参数
- laravel - Maatwebsite Laravel Excel 重复记录被删除
- c - select() 是否保证所有数据都可用于从套接字读取或仅部分数据
- kubernetes - 用于重新部署的 Terraform ConfigMap 哈希
- url - 从 URL 填写表格 - postget 或如何?
- azure - 使用 Azure 数据工厂 V2 按 LastModifiedDate 复制新的和更改的文件
- php - 如何使用 Axios 将 ReactJs 中的表单提交到 PHP 处理程序(PHP miler 到电子邮件表单数据)?
- java - 正则表达式根据第一次出现的允许分隔符拆分过滤器字符串并获取键、运算符、值
- php - 在 Wordpress 主题中更改“加载更多”按钮的最佳方法是什么?
- swift - 在 Internet 连接可用时将多个存储的数据从已保存的本地数据库发送到服务器 iOS