我编码拍照并将其保存在外部存储器中。在我的平板电脑上使用SD卡很好。但是,我的手机有些麻烦,不支持SD卡。任何人都可以帮我写代码来保存在内部存储。谢谢!
((Button) findViewById(R.id.btnTaken))
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
takeImage();
}
});
protected void takeImage() {
Log.i(TAG,"onClick event");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
String currentDateandTime = sdf.format(new Date());
String fileName = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/Image_" + currentDateandTime + ".jpg";
mCamera.takePicture(fileName);
Toast.makeText(this, fileName + " saved", Toast.LENGTH_SHORT).show();
}
public void takePicture(final String fileName) {
Log.i(TAG, "Tacking picture");
PictureCallback callback = new PictureCallback() {
private String mPictureFileName = fileName;
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.i(TAG, "Saving a bitmap to file");
Bitmap picture = BitmapFactory.decodeByteArray(data, 0, data.length);
try {
FileOutputStream out = new FileOutputStream(mPictureFileName);
picture.compress(Bitmap.CompressFormat.JPEG, 90, out);
picture.recycle();
mCamera.startPreview();
} catch (Exception e) {
e.printStackTrace();
}
}
};
Camera.ShutterCallback shutter = new Camera.ShutterCallback() {
public void onShutter() {
Log.e(getClass().getSimpleName(), "SHUTTER CALLBACK");
}
};
Camera.PictureCallback raw = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera c) {
Log.e(getClass().getSimpleName(), "PICTURE CALLBACK RAW: " + data);
mCamera.startPreview();
}
};
mCamera.takePicture(shutter, raw, callback) ;
}
答案 0 :(得分:-4)
这很简单..而不是外部文件路径名。你应该给出内部文件路径名称..这是FileOutputStream out = new FileOutputStream(new File(getFilesDir()+File.separator+"MyFile.jpg"));
..试试吧..它运作良好
和
还有一点,您应该将内部存储的权限添加到Android Manifest.xml文件
中<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>