1、setParameters failed解決方法
2、無預覽拍照
客戶的需求千奇百怪,有要靜默安裝功能的,下載編譯好的ROM,在ROM中加入自己的應用程序實現靜默安裝,為了盈利好無恥啊。現在要實現無預覽拍照功能,哥們又哭笑不得,客戶是上帝,木有辦法。據說無預覽拍照在Android 4.1中無法實現了。因為牽涉到用戶隱私問題。
看代碼吧,其實實現特簡單。主要是思路問題。
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 SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
public CameraPreview(Context context) {
super(context);
mHolder = getHolder();
mHolder.addCallback(this);
//mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // 4.0+ auto
}
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open(1);
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException exception) {
mCamera.release();
mCamera = null;
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w,
int h) {
Camera.Parameters parameters = mCamera.getParameters();
Size size = parameters.getPreviewSize();
Log.e(VIEW_LOG_TAG, "size.width="+size.width+"size.height="+size.height);
// setPreviewSize 寬高不能大於 parameters.getPreviewSize(),否者會出現java.lang.RuntimeException: setParameters failed錯誤
//parameters.setPreviewSize(800, 600);
mCamera.setParameters(parameters);
mCamera.startPreview();
}
}
}
為了防止一些侵犯用戶隱私和一些非法吸費等軟件,安裝應用程序時千萬注意看應用的權限;喜歡折騰刷機的朋友盡量從官網下載ROM。應用程序盡量從Google Play下載。
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11