所以我有一个蓝牙设备,我听按钮按下设备,我按下按钮时尝试拍照。问题是我找不到任何解决办法。
} else if (destination.equals(APPLICATION_ACTION_DESTINATION_OPEN_CAMERA)) {
Intent intent1 = new Intent("android.intent.action.CAMERA_BUTTON");
intent1.putExtra("android.intent.extra.KEY_EVENT", new KeyEvent(0, KeyEent.KEYCODE_CAMERA));
context.sendOrderedBroadcast(intent1, null);
intent1 = new Intent("android.intent.action.CAMERA_BUTTON");
intent1.putExtra("android.intent.extra.KEY_EVENT", new KeyEvent(1, KeyEvent.KEYCODE_CAMERA));
context.sendOrderedBroadcast(intent1, null);
} else if (destination.equals(APPLICATION_ACTION_DESTINATION_TAKE_PHOTO)) {
}
我发现这样做的唯一方法是使用:
Instrumentation.sendKeyDownUpSync();
问题是我需要注册INJECT_EVENTS权限,该权限仅授予系统应用程序。
有人设法做到了吗?
答案 0 :(得分:0)
您可以使用此意图拍摄照片
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
您可以找到更多信息here
并在音量键上调用摄像头,您只需在Activity
中覆盖此方法@Override
public boolean dispatchKeyEvent(KeyEvent event) {
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
case KeyEvent.KEYCODE_VOLUME_DOWN:
return true;
default:
return super.dispatchKeyEvent(event);
}
}
我已经习惯了它,完美无缺
完整演示:
public class CameraDemoActivity extends Activity {
int TAKE_PHOTO_CODE = 0;
public static int count = 0;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Here, we are making a folder named Pitures to store
// pics taken by the camera using this application
final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/Pitures/";
File newdir = new File(dir);
newdir.mkdirs();
Button capture = (Button) findViewById(R.id.btnCapture);
capture.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Here, the counter will be incremented each time, and the
// picture taken by camera will be stored as 1.jpg,2.jpg and so on
count++;
String file = dir+count+".jpg";
File newfile = new File(file);
try {
newfile.createNewFile();
}
catch (IOException e)
{
}
Uri outputFileUri = Uri.fromFile(newfile);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
Log.d("CameraDemo", "Pic saved");
}
}
}