我想通过应用程序用手机拍照并将图像保存在手机上。 我已经尝试了在stackoverflow问题上提出的许多解决方案,但它没有标记,所以我构建了一个方法,用正确的名称保存文件......但文件是空的(0kb)!
这是我的代码
public class GameActivity extends Activity implements SurfaceHolder.Callback/*,Camera.PictureCallback*/ {
private Camera camera;
private SurfaceView surfaceCamera;
public Handler handler = new Handler();
private boolean isPreview=false;
private SurfaceHolder holder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gameactivity);
surfaceCamera = (SurfaceView) findViewById(R.id.surfaceViewCamera);
getWindow().setFormat(PixelFormat.UNKNOWN);
holder = surfaceCamera.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
ImageView image = (ImageView) findViewById(R.id.imageView3);
image.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
camera.takePicture(myShutterCallback, myPictureCallback_RAW, myPictureCallback_JPG);
return false;
}
Camera.ShutterCallback myShutterCallback = new Camera.ShutterCallback() {
public void onShutter() {
// TODO bl
}
};
PictureCallback myPictureCallback_RAW = new PictureCallback() {
public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
}
};
PictureCallback myPictureCallback_JPG = new PictureCallback(){
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "/KersplattFolder");
imagesFolder.mkdirs();
String fileName = "image.jpg";
File output = new File(imagesFolder, fileName);
try {
FileOutputStream fos = new FileOutputStream(output);
fos.write(data[0]);
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(GameActivity.this,
"Image saved: ",
Toast.LENGTH_LONG).show();
camera.startPreview();
}
};
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
if (isPreview) {
camera.stopPreview();
}
Camera.Parameters p = camera.getParameters();
p.setPreviewSize(width,height);
camera.setParameters(p);
try {
camera.setPreviewDisplay(holder);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
camera.startPreview();
isPreview=true;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
isPreview=false;
camera.release();
}
EDIT1 :当我输入数据而不是数据[0]时,我也保存了黑色图像,但文件有重量,所以我猜真实的图像在某个地方......
EDIT2 我添加了代码
File output = new File(imagesFolder, fileName);
Uri outputFileUri = Uri.fromFile(output);
String filePath = outputFileUri.getPath();
File file= new File(filePath);
try {
FileOutputStream fos = new FileOutputStream(file,true);
fos.write(data);
fos.close();
}
还有黑色图片
答案 0 :(得分:0)
您是否在Android清单中定义了所需权限,如here所述?
<manifest ... >
<uses-feature android:name="android.hardware.camera" />
...
</manifest ... >
<强>更新强> 好的,删除数据后的括号:数据而不是数据[]。然后它应该工作。
答案 1 :(得分:0)
您的代码:fos.write(data [0]);是错的。我很惊讶你没有得到例外
这是我用来在拍照后保存字节数组的代码。 “data”是字节数组。
String filePath = outputFileUri.getPath(); // .getEncodedPath();
File file = new File(filePath);
FileOutputStream os;
try {
os = new FileOutputStream(file, true);
os.write(data);
os.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}