简体   繁体   English

从相机捕获图像并保存到SD卡

[英]Capture image from camera and save to SD card

I am trying to capture image and then save it into the SDCard .But the camera is getting on but the image is not saving on the location and even it is not creating the folder.Please check the below code to on the camera capture a image and then save it . 我正在尝试捕获图像,然后将其保存到SDCard中。但是相机正在启动,但是图像没有保存在该位置上,甚至没有创建文件夹。请在相机上捕获以下代码以捕获图像然后保存。

public class TakePicture extends Activity {
int TAKE_PHOTO_CODE = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_take_picture);

    //here,we are making a folder named picFolder to store pics taken by the camera using this application
        final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/"; 
        File newdir = new File(dir); 
        newdir.mkdirs();

        Button capture = (Button) findViewById(R.id.btnCapture);
        capture.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // here,counter will be incremented each time,and the picture taken by camera will be stored as 1.jpg,2.jpg and likewise.

            String file = System.currentTimeMillis() + ".jpg";
            File newfile = new File(file);
            try {
                newfile.createNewFile();
            } catch (IOException e) {}       

            Uri outputFileUri = Uri.fromFile(newfile);

            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

            startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
        }
    });
}

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
            Log.d("CameraDemo", "Pic saved");
            Log.d("data-----",data.toString());


        }else{
            Log.d("CameraDemo", "Pics Not Saved ");
        }
}

}

I know this isn't exactly an answer to your question, but wouldn'nt it be easier to use the stock camera application? 我知道这并不完全是您的问题的答案,但是使用库存相机应用程序会不会更容易? You can access it using this code in your activity: 您可以在活动中使用以下代码访问它:

            @Override
            public void onCreate(Bundle savedInstanceState)
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.capture);

                Button capture = (Button) findViewById(R.id.capture_button);
                capture.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        // We use the stock camera app to take a photo
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri());
                        startActivityForResult(intent, TAKE_PHOTO_CODE);
                    }
                });
            }

            @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);

                if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
                    Uri imagePath = getImageUri();

                    doSomething();
                }
            }

            /**
             * Get the uri of the captured file
             * @return A Uri which path is the path of an image file, stored on the dcim folder
             */
            private Uri getImageUri() {
                // Store image in dcim
                File file = new File(Environment.getExternalStorageDirectory() + "/DCIM", CAPTURE_TITLE);
                Uri imgUri = Uri.fromFile(file);

                return imgUri;
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PhoneGap - Android - 如何在SD卡中保存摄像头的捕获图像 - PhoneGap - Android - how to save the capture image from camera in the sd card 从本机相机捕获之后但在将其保存到SD卡之前调整图像大小 - Resize image after capture it from native camera but before save it to SD card 从前置摄像头捕获图片并将其保存到SD卡中而不进行预览 - Capture picture from front camera and save it on SD card without preview Android将自定义相机中的图像保存到SD卡上的自定义文件夹中 - Android Save Image from Custom Camera to Custom Folder on SD Card 如何捕获图像并将其保存在SD卡中 - How to capture the image and save it in sd card 捕获图像并保存到Android中的SD卡时图像拉伸 - Image stretched when I capture image and save to SD card in Android 从ImageView获取图像并将其保存到SD卡 - get the Image from ImageView and save it to SD card Android相机 - 将图像保存到SD卡中的新文件夹中 - Android Camera - Save image into a new folder in SD Card 将相机图像保存到 SD 卡,检索并上传到 Firestore 存储 - Save camera image to SD card, retrieve and upload to Firestore Storage 从SD卡拍摄图像,将其压缩,然后保存回SD? - Take image from SD card, compress it, and save it back to SD?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM