How to save a picture in a specific folder just for my app (correctly)?

断了今生、忘了曾经 提交于 2019-12-08 13:46:57

问题


The problem is that my app saves the picture twice; one in the camera folder and the other in the folder I specified. but when I tested the app on another device, that didn't happen!

//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;
    }

So, I want a solution for this, and I also want to save the picture in a folder for my app inside the Pictures Directory.. thanks


回答1:


There is no solution for it, other than to not use ACTION_IMAGE_CAPTURE.

ACTION_IMAGE_CAPTURE delegates picture-taking to an arbitrary third-party camera app. There are dozens, if not hundreds, of these pre-installed on devices. There are hundreds more available for download from the Play Store and elsewhere. What they do in response to that Intent action is up to them. Ideally, they would only store the image in the location specified by EXTRA_OUTPUT. However, there is no requirement that they behave that way. Some camera apps will store the image twice, once in its normal location and once in the EXTRA_OUTPUT. Some will ignore EXTRA_OUTPUT entirely.

If this concerns you, do not use ACTION_IMAGE_CAPTURE. Use a library — CameraX, Fotoappart, CameraKit-Android, etc. — to take the photos within your own app.




回答2:


Call below method from OnActivityResult.

private void saveImage(Bitmap  image, String fileName) {

    File direct = new File(Environment.getExternalStorageDirectory() + "/DirName");
    if (!direct.exists()) {
        File directory = new File("/sdcard/DirName/");
        directory.mkdirs();
    }

    File file = new File(new File("/sdcard/DirName/"), fileName);
    if (file.exists()) {
        file.delete();
    }
    try {
        FileOutputStream out = new FileOutputStream(file);
        image.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}


来源:https://stackoverflow.com/questions/57660896/how-to-save-a-picture-in-a-specific-folder-just-for-my-app-correctly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!