0

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

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

1 回答 1

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
        }
    }
});
于 2013-05-22T16:56:41.203 回答