Android摄像头 - 保存图片到SD卡中一个新的文件夹文件夹、摄像头、图片、Android

2023-09-12 08:00:01 作者:-冷心冷血冷感情°

我有一个非常简单的应用程序,它目前需要的图片,然后保存图像。目前的问题是,由于某种原因,我无法找到该图像被保存到手机上。

I have a very simple APP which at the moment takes a picture and then saves the image. The problem at the moment is that for some reason i cannot find where the image is being saved to on the phone.

什么我试图做的最终结果是,当一个画面了,然后被保存到已在SD卡中创建一个新的文件夹中的形象,但如果该文件夹不存在,它必须是发(程序自动),可以将图像保存之前。

The finished outcome with what i am trying to do is when a picture is took the image then gets saved into a new folder that has been created on the SD Card, but if the folder does not already exist it will have to be made (automaticlly) before the image can be saved.

我曾尝试使用了答案in这个问题,但似乎无法incoporate它没有得到错误 imageIntent解决不了

I have tried to use the answer in this question but cannot seem to incoporate it without getting the error imageIntent cannot be resolved

编辑:图片,现在保存到SD卡,并创建文件夹,但覆盖透水形象,我需要它来保存多张图片,如果任何人有code已被更新任何建议

这是我的code的一个片段:

This is a snippet of my code:

PictureCallback myPictureCallback_JPG = new PictureCallback(){

    public void onPictureTaken(byte[] arg0, Camera arg1) {
        // TODO Auto-generated method stub
        /*Bitmap bitmapPicture 
            = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);  */


        int imageNum = 0;
        Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Punch");
        imagesFolder.mkdirs(); // <----
        String fileName = "image_" + String.valueOf(imageNum) + ".jpg";
        File output = new File(imagesFolder, fileName);
        while (output.exists()){
            imageNum++;
            fileName = "image_" + String.valueOf(imageNum) + ".jpg";
            output = new File(imagesFolder, fileName);
        }
        Uri uriSavedImage = Uri.fromFile(image);
        imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);


        OutputStream imageFileOS;
        try {
            imageFileOS = getContentResolver().openOutputStream(uriSavedImage);
            imageFileOS.write(arg0);
            imageFileOS.flush();
            imageFileOS.close();

            Toast.makeText(AndroidCamera.this, 
                    "Image saved: ", 
                    Toast.LENGTH_LONG).show();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        camera.startPreview();
    }};

修改

code已被更新,现在保存多个图像在SD卡中创建一个新的文件夹。

Code has been updated to now save multiple images in a new folder created on SD Card.

推荐答案

您可以保存多个图像。你每次当你拍摄的照片所在的文件夹,一次又一次重建的时间做了一个错误。

You can save multiple image. you did one mistake every time when you are capturing the photo the folder is recreating again and again.

所以你要检查该文件夹是否存在与否。你必须添加只有一行code

so you have to check whether the folder is already exist or not. you have to add only one line of code

if (!imagesFolder.exists()) {
imagesFolder.mkdirs();
}
else { //do nothing}