拍摄的图像不会保存到SD卡中

时间:2015-04-24 14:31:59

标签: android android-sdcard

我开始camera意图:

    public class OngletParcelleListeBatiments extends Activity {

        ...

        public void launchCamera(View v) { // called on click of a button

            Uri fileUri = CameraUtil.getOutputMediaFileUri();

            if (fileUri.equals(Uri.EMPTY))

                displaySDError(); // a DialogView

            else {

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

                startActivityForResult(intent, CameraUtil.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

            }

        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == CameraUtil.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
                if (resultCode == RESULT_OK) {
                    // Image captured and saved to fileUri specified in the Intent

                } else if (resultCode == RESULT_CANCELED) {
                    // User cancelled the image capture
                } else {
                    // Image capture failed, advise user
                }
            }
        }

        ...
    }

    public class CameraUtil {

        public static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;

        private static File getOutputMediaFile() {

            if (Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {

                File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "PhotosProjetImpotsLocaux"); 
// I already set the first argument to Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) but it didn't work !

                if (! mediaStorageDir.exists()){
                    if (! mediaStorageDir.mkdirs()) {
                        return null;
                    }
                }

                String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
                File mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".png");

                return mediaFile;

            }

            return null;
        }

        public static Uri getOutputMediaFileUri() {
            Uri ret = Uri.EMPTY;
            try {
                ret = Uri.fromFile(getOutputMediaFile());
            } catch (NullPointerException e) {
                ret = Uri.EMPTY;
            }
            return ret;
        }

    }

SD卡已插入平板电脑并已安装。问题是在捕获照片后的运行时,图像在internal storage内但不在SD卡内!那我的代码有什么问题?

1 个答案:

答案 0 :(得分:0)

变化:

 new File(Environment.getExternalStorageDirectory(), "PhotosProjetImpotsLocaux"); 

new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/PhotosProjetImpotsLocaux/" );

编辑: 并将其添加到清单

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />