Android 如何仅为我的应用程序将图片保存在特定文件夹中(正确)?

Android 如何仅为我的应用程序将图片保存在特定文件夹中(正确)?,android,storage,Android,Storage,问题是我的应用程序保存了两次图片;一个在相机文件夹中,另一个在我指定的文件夹中。但当我在另一台设备上测试该应用程序时,却没有发生这种情况 //lunch the camera and make a file to save the image in and pass it with the camera intent public void lunchCamera() { Intent takePictureIntent = new Intent(MediaStore.A

问题是我的应用程序保存了两次图片;一个在相机文件夹中,另一个在我指定的文件夹中。但当我在另一台设备上测试该应用程序时,却没有发生这种情况

//lunch the camera and make a file to save the image in and pass it with the camera intent
    public void lunchCamera() {
        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();
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this,
                        "com.ziad.sayit",
                        photoFile);

                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }
        }
    }

    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "SayIt_" + timeStamp + "_";
        File storageDir = getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File imageFile = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = imageFile.getAbsolutePath();
        return imageFile;
    }

因此,我想要一个解决方案,我还想将图片保存在我的应用程序的文件夹中,在图片目录中。。谢谢

除了不使用
动作\图像\捕获
之外,没有其他解决方案

ACTION\u IMAGE\u CAPTURE
将拍照委托给任意第三方相机应用程序。有几十个,如果不是几百个的话,这些设备是预先安装在设备上的。还有数百个可从Play Store和其他地方下载。他们如何应对
意图
行动取决于他们自己。理想情况下,它们只将图像存储在
EXTRA\u OUTPUT
指定的位置。然而,没有要求他们这样做。一些相机应用程序会将图像存储两次,一次存储在其正常位置,一次存储在
额外输出中。有些人会完全忽略额外的输出


如果这与您有关,请不要使用
ACTION\u IMAGE\u CAPTURE
。使用库(CameraX、Fotoappart、CameraKit Android等)在您自己的应用程序中拍照。

除了不使用
操作图像捕获之外,没有其他解决方案

ACTION\u IMAGE\u CAPTURE
将拍照委托给任意第三方相机应用程序。有几十个,如果不是几百个的话,这些设备是预先安装在设备上的。还有数百个可从Play Store和其他地方下载。他们如何应对
意图
行动取决于他们自己。理想情况下,它们只将图像存储在
EXTRA\u OUTPUT
指定的位置。然而,没有要求他们这样做。一些相机应用程序会将图像存储两次,一次存储在其正常位置,一次存储在
额外输出中。有些人会完全忽略额外的输出


如果这与您有关,请不要使用
ACTION\u IMAGE\u CAPTURE
。使用库(CameraX、Fotoappart、CameraKit Android等)在自己的应用程序中拍照。

从OnActivityResult调用下面的方法

private void saveImage(位图图像,字符串文件名){
File direct=新文件(Environment.getExternalStorageDirectory()+“/DirName”);
如果(!direct.exists()){
文件目录=新文件(“/sdcard/DirName/”);
mkdirs()目录;
}
File File=新文件(新文件(“/sdcard/DirName/”,文件名);
if(file.exists()){
delete();
}
试一试{
FileOutputStream out=新的FileOutputStream(文件);
image.compress(Bitmap.CompressFormat.JPEG,100,out);
out.flush();
out.close();
}捕获(例外e){
e、 printStackTrace();
}
}

从OnActivityResult调用下面的方法

private void saveImage(位图图像,字符串文件名){
File direct=新文件(Environment.getExternalStorageDirectory()+“/DirName”);
如果(!direct.exists()){
文件目录=新文件(“/sdcard/DirName/”);
mkdirs()目录;
}
File File=新文件(新文件(“/sdcard/DirName/”,文件名);
if(file.exists()){
delete();
}
试一试{
FileOutputStream out=新的FileOutputStream(文件);
image.compress(Bitmap.CompressFormat.JPEG,100,out);
out.flush();
out.close();
}捕获(例外e){
e、 printStackTrace();
}
}

但是如果照片还没有拍摄,我如何获取位图和字符串文件名?如果照片还没有拍摄,我如何获取位图和字符串文件名?好的,那么,我可以重构代码,使相机应用程序仅将照片保存在默认目录中吗?比如,我怎样才能让照片进入相机文件夹,只保存一次?@ZiadH.:“我可以重构代码,让相机应用程序只将照片保存在默认目录中吗?”——我对此表示怀疑。您需要与这个特定相机应用程序的开发人员交谈,并与他们一起找出是否有办法创建一个
动作\u图像\u捕获
意图
,从而克服他们的缺陷。你需要这样做。好吧,那么,我可以重构代码,让相机应用程序只在默认目录下保存照片吗?比如,我怎样才能让照片进入相机文件夹,只保存一次?@ZiadH.:“我可以重构代码,让相机应用程序只将照片保存在默认目录中吗?”——我对此表示怀疑。您需要与这个特定相机应用程序的开发人员交谈,并与他们一起找出是否有办法创建一个
动作\u图像\u捕获
意图
,从而克服他们的缺陷。你需要这样做。