Android - Save images in an specific folder

后端 未结 5 1505
执笔经年
执笔经年 2020-11-29 22:01

I need to save the pictures taken with my app in an specific folder. I\'ve read many solutions to this problem but I couldn\'t make any of them work so I ask for help.

5条回答
  •  悲哀的现实
    2020-11-29 22:38

    I have used mdDroid's code like this:

    public void startCamera() {
        // Create photo
        newPhoto = new Photo();
        newPhoto.setName(App.getPhotoName());
    
        //Create folder !exist
        String folderPath = Environment.getExternalStorageDirectory() + "/PestControl";
        File folder = new File(folderPath);
        if (!folder.exists()) {
            File wallpaperDirectory = new File(folderPath);
            wallpaperDirectory.mkdirs();
        }
        //create a new file
        newFile = new File(folderPath, newPhoto.getName());
    
        if (newFile != null) {
            // save image here
            Uri relativePath = Uri.fromFile(newFile);
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, relativePath);
            startActivityForResult(intent, CAMERA_REQUEST);
        }
    }
    

提交回复
热议问题