0
我已經嘗試了很多相機示例爲Android,但this(http://androideity.com/2011/10/08/hello-camera/)是冷杉我得到保存圖像並在畫廊上查看它。但我想知道的是如何更改保存圖像的文件夾。我想實際上它可以將它們保存在sdcard0/DCIM
之上。更改目錄文件 - 安卓相機
下面是主要活動:
public class Foto extends Activity implements SurfaceHolder.Callback{
private LayoutInflater myInflater = null;
Camera myCamera;
byte[] tempdata;
boolean myPreviewRunning = false;
private SurfaceHolder mySurfaceHolder;
private SurfaceView mySurfaceView;
ImageButton takePicture;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.foto);
mySurfaceView = (SurfaceView) findViewById(R.id.surface);
mySurfaceHolder = mySurfaceView.getHolder();
mySurfaceHolder.addCallback(this);
mySurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
myInflater = LayoutInflater.from(this);
View overView = myInflater.inflate(R.layout.segundacapa,null);
this.addContentView(overView, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
takePicture = (ImageButton) findViewById(R.id.button);
takePicture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myCamera.takePicture(myShutterCallback, myPictureCallback, myJpeg);
}
});
}
ShutterCallback myShutterCallback = new ShutterCallback() {
@Override
public void onShutter() {
}
};
PictureCallback myPictureCallback = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera myCamera) {
// TODO Auto-generated method stub
}
};
PictureCallback myJpeg = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera myCamera) {
// TODO Auto-generated method stub
if(data != null){
tempdata = data;
done();
}
}
};
void done(){
Bitmap bm = BitmapFactory.decodeByteArray(tempdata, 0, tempdata.length);
String url = Images.Media.insertImage(getContentResolver(), bm, null, null);
bm.recycle();
Bundle bundle = new Bundle();
if(url != null){
bundle.putString("url",url);
Intent mIntent = new Intent();
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
Toast.makeText(this, "Picture saved on" + url, Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(this, "Picture can not be saved", Toast.LENGTH_SHORT).show();
}
finish();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
try{
if(myPreviewRunning){
myCamera.stopPreview();
myPreviewRunning = false;
}
Camera.Parameters p = myCamera.getParameters();
p.setPreviewSize(width,height);
myCamera.setParameters(p);
myCamera.setPreviewDisplay(holder);
myCamera.startPreview();
myPreviewRunning = true;
}catch(Exception e){}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
myCamera = Camera.open();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
myCamera.stopPreview();
myPreviewRunning = false;
myCamera.release();
myCamera = null;
}
}
還有與SurfaceView
(稱爲foto.xml
),另一個稱爲segundacapa.xml
與按鈕的XML佈局。
非常感謝!