将图像,音频和视频保存到SD卡

时间:2012-02-16 22:42:08

标签: java android android-activity android-intent android-camera

我无法理解如何将文件保存到SD卡。对于我的相机类,我拍摄一张照片,而不是创建一个新目录来保存它。它创建了文件夹,但图像不在文件夹中?请帮忙。

if (items[which].equals("Picture")) {

                            Intent cameraIntent = new Intent(
                                    MediaStore.ACTION_IMAGE_CAPTURE);
                            startActivityForResult(cameraIntent,
                                    PICTURE_ACTIVITY);

                            File imagesFolder = new File(Environment
                                    .getExternalStorageDirectory(),
                                    "SeccyPhotos");
                            imagesFolder.mkdirs(); // <----
                            File image = new File(imagesFolder, "image_001.jpg");
                            Uri uriSavedImage = Uri.fromFile(image);
                            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                                    uriSavedImage);
                        }

我拍摄相机然后拍照后它应保存到文件夹中。

编辑: 视频会和相机一样吗?

        Intent cameraIntent = new Intent(
                                MediaStore.ACTION_VIDEO_CAPTURE);
                        startActivityForResult(cameraIntent,
                                PICTURE_ACTIVITY);

                        File videosFolder = new File(Environment
                                .getExternalStorageDirectory(),
                                "cangryVideos");
                        videosFolder.mkdirs(); // <----
                        File video = new File(videosFolder, "Video_001.3gp");
                        Uri uriSavedvideo = Uri.fromFile(video);
                        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                                uriSavedvideo);
                        startActivityForResult(cameraIntent,
                                PICTURE_ACTIVITY);

1 个答案:

答案 0 :(得分:1)

在将图像文件URI设置为startActivityForResult(cameraIntent, PICTURE_ACTIVITY);之前,您正在调用cameraIntent

您的代码应为:

if (items[which].equals("Picture")) {
   Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

   File imagesFolder = new File(Environment.getExternalStorageDirectory(),
        "SecExPhotos");
   imagesFolder.mkdirs(); // <----
   File image = new File(imagesFolder, "image_001.jpg");
   Uri uriSavedImage = Uri.fromFile(image);
   cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,uriSavedImage);
   startActivityForResult(cameraIntent,PICTURE_ACTIVITY);
}

这可以解决您的问题。