如何在不询问用户的情况下自动保存相机中的照片(方便)

时间:2013-05-22 16:46:11

标签: android camera photos autosave


您好 我正在寻找一种直接从相机设备保存照片的方法,而无需要求用户保存。我没有实现Camera类或覆盖它。我只是使用上面的代码。你有什么想法怎么做吗?

TextReader tr = new TextReader(TextReader.DIRECTORY_EMPRESAS);
String path = TextReader.PARENT_PATH + "/" + TextReader.DIRECTORY_IMAGES;
String dataAtual = new SimpleDateFormat("yyMMddHHmmss").format(new Date());

if(tr.verificaDiretorios(path)){
    String pictureName = dataAtual + DADOS.get(0) + ".jpg";
    File pathEmpresa = new File(path + "/" + TextReader.FILE_NAME);
    File imageFile;

    if(pathEmpresa.exists()){
         imageFile = new File(pathEmpresa, pictureName);        
         Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
         i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
         //startActivity(i);
         startActivityForResult(i, 2);

    }else{
        if(pathEmpresa.mkdir()){        
            imageFile = new File(pathEmpresa, pictureName);     
            Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
            //startActivity(i);
            startActivityForResult(i, 2);
        }else{
            throw new IllegalStateException("Não foi possivel criar o diretorio: " +         pathEmpresa);
        }
}    

1 个答案:

答案 0 :(得分:0)

使用Intent无法做到这一点。您需要使用Camera类。

请参阅文档:

http://developer.android.com/reference/android/hardware/Camera.html


快速示例:

Camera camera = camera.open();
camera.takePicture(null, null, new Camera.PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        if (data != null) {
            Bitmap picture = BitmapFactory.decodeByteArray(data);
            File f = new File(Environment.getExternalStorageDirectory(), "mydir");
            f.mkdirs();//Grab file directory
            f = new File(f, "mypicturefilename"); //Grab picture file location
            BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(f));
            picture.compress(CompressFormat.JPEG, os);//Write image to file
            picture.recycle(); //Clean up
        }
    }
});