Android4.1 无预览拍照
1、setParameters failed解决方法2、无预览拍照
客户的需求千奇百怪,有要静默安装功能的,下载编译好的ROM,在ROM中加入自己的应用程序实现静默安装,为了盈利好易做图啊。现在要实现无预览拍照功能,哥们又哭笑不得,客户是上帝,木有办法。据说无预览拍照在4.1中无法实现了。因为牵涉到用户隐私问题。
看代码吧,其实实现特简单。主要是思路问题。
[java]
<SPAN style="FONT-SIZE: 14px">public class NoPreviewTakePicActivity extends Activity{
private Camera mCamera;
private CameraPreview mPreview;
private ToneGenerator mTone;
private static final int OPTION_SNAPSHOT = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPreview = new CameraPreview(this);
setContentView(R.layout.main);
addContentView(mPreview, new LayoutParams(1, 1));// 在Layout中加入了一个宽高为1的一个预览区域。但用户确实看不到,就莫名其妙的拍照了。
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
switch(itemId){
case OPTION_SNAPSHOT:
mCamera.takePicture(shutterCallback, null, jpegCallback);
break;
}
return true;
}
private PictureCallback jpegCallback = new PictureCallback(){
public void onPictureTaken(byte[] data, Camera camera) {
Parameters ps = camera.getParameters();
if(ps.getPictureFormat() == PixelFormat.JPEG){
String path = save(data);
Uri uri = Uri.fromFile(new File(path));
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.setDataAndType(uri, "image/jpeg");
startActivity(intent);
}
}
};
private ShutterCallback shutterCallback = new ShutterCallback(){
public void onShutter() {
if(mTone == null){
mTone = new ToneGenerator(AudioManager.STREAM_MUSIC,ToneGenerator.MAX_VOLUME);
}
mTone.startTone(ToneGenerator.TONE_PROP_BEEP2);
}
};
private String save(byte[] data){
String path = "/mnt/sdcard/"+System.currentTimeMillis()+".jpg";
try {
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
String storage = Environment.getExternalStorageDirectory().toString();
StatFs fs = new StatFs(storage);
long available = fs.getAvailableBlocks()*fs.getBlockSize();
if(available<data.length){
return null;
}
File file = new File(path);
if(!file.exists()){
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(data);
fos.close();
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return path;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, OPTION_SNAPSHOT, 0, R.string.snapshot);
return super.onCreateOptionsMenu(menu);
}
class CameraPreview extends Su易做图ceView implements Su易做图ceHolder.Callback {
Su易做图ceHolder mHolder;
public CameraPreview(Context context) {
super(context);
mHolder = getHolder();
mHolder.addCallback(this);
//mHolder.setType(Su易做图ceHolder.SURFACE_TYPE_PUSH_BUFFERS); // 4.0+ auto
}
public void su易做图ceCreated(Su易做图ceHolder holder) {
mCamera = Camera.open(1);
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException exception) {
mCamera.release();
&
补充:移动开发 , Android ,