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

时间:2013-07-05 10:55:21

标签: android

我正在尝试捕捉图像,然后将其保存到SD卡中。但相机正在开启,但图像未保存在该位置,甚至没有创建文件夹。请检查以下代码在相机上捕获图像然后保存。

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 ");
        }
}

}

1 个答案:

答案 0 :(得分:1)

我知道这不是你问题的答案,但使用相机应用程序会不会更容易?您可以在活动中使用此代码访问它:

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