将捕获的图像保存到SD卡

时间:2017-03-01 12:12:00

标签: android

我想将已捕获的imagecamera保存到extSdcard,但问题images存储在internal memory的手机android 4.4.4(kitkat)android 5.1.1 1}}和android 4.2,但extsdcard图片已存储在__set()中,请您对此有何疑问?

2 个答案:

答案 0 :(得分:0)

根据Document

  

不得允许应用程序写入辅助外部存储   设备,特定于包装

您可以写在primary external storage上的应用专用文件夹,即/sdcard/Android/data/package/secondary external storage上的应用专用文件夹,即/extSdcard/Android/data/package/WRITE_EXTERNAL_STORAGE即可写入primary external storage上的所有位置,即/sdcard/,但您无法写入secondary external storage上的任何位置(应用特定文件夹除外)。

答案 1 :(得分:0)

所以我必须创建一个文件夹,其中包含我的应用程序包的名称,但在我的应用程序的代码中,我必须进行更改?这是我的代码

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


                        mCamera.takePicture(null, null, mPicture);



}

/**
 * Helper method to access the camera returns null if it cannot get the
 * camera or does not exist
 *
 * @return
 */
private Camera getCameraInstance() {
    Camera camera = null;
    try {
        camera = Camera.open();
    } catch (Exception e) {
        // cannot get camera or does not exist
    }
    return camera;
}

PictureCallback mPicture = new PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        File pictureFile = getOutputMediaFile();

        if (pictureFile == null) {
            return;
        }
        try {
            pictureFile.createNewFile();
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
        } catch (FileNotFoundException e) {

        } catch (IOException e) {
        }
    }

};

    private static File getOutputMediaFile() {
        File mediaStorageDir = new File(Environment.getExternalStorageDirectory() + "/MyCameraApp/");
        if (!mediaStorageDir.exists()) {
            mediaStorageDir.mkdir();
        }
        return mediaStorageDir;
    }


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.

    return true;
}


private String currentDateFormat(){
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HH_mm_ss");
    String  currentTimeStamp = dateFormat.format(new Date());
    return currentTimeStamp;
}

}