需要获取权限:
第一步
if (mCamera != null) {
mCamera.takePicture(null, null, mPicture);
}
第二步
/**
* 保存相机图片
*/
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
String randomLivePath = FileUtils.getRandomLivePath(StartLiveActivity.this);
Log.e("test", "onPictureTaken: " + randomLivePath);
File pictureFile = new File(randomLivePath);
if (pictureFile == null) {
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
}
};
public static String getRandomLivePath(Context context){
// String path = Environment.getExternalStorageDirectory().getPath();
String path;
// if(context.getExternalCacheDir()!=null){
// path = context.getExternalCacheDir().getPath();
// }else {
path = Environment.getExternalStorageDirectory().getPath();
// }
path = path+"/renshine";
Log.e("test", "getRandomLivePath: "+path );
File file = new File(path);
if(!file.exists()){
boolean tf =file.mkdirs();
}
path+="/"+System.currentTimeMillis()+".jpg";
file = new File(path);
if(!file.exists()){
try {
boolean tf = file.createNewFile();
}catch (Exception e){
e.printStackTrace();
}
}
return path;
}