我正在使用Android Studio创建应用程序。我希望系统能够访问手机的摄像头。它在模拟器上工作,我能够拍摄照片并将图片设置为ImageView中的图像。
但是,我开始使用手机运行,但相机无法正常工作。通常当我点击ImageButton时,相机会打开,但现在,它会将我重定向回我之前打开的页面。
这是我的代码:
public class DriverActivity extends AppCompatActivity {
Button btnDriverBack, btnDriverSubmit;
ImageButton ibtnDriverPhoto;
ImageView imageDriver;
EditText etDriverFirstName, etDriverLastName, etDriverMiddleName, etDriverBDay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_driver);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ibtnDriverPhoto = (ImageButton) findViewById(R.id.ibtnDriverPhoto);
imageDriver = (ImageView) findViewById(R.id.imageDriver);
etDriverFirstName = (EditText) findViewById(R.id.etDriverFirstName);
etDriverMiddleName = (EditText) findViewById(R.id.etDriverMiddleName);
etDriverLastName = (EditText) findViewById(R.id.etDriverLastName);
etDriverBDay = (EditText) findViewById(R.id.etDriverBDay);
ibtnDriverPhoto.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent camera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(camera, 1);
}
});
btnDriverBack = (Button) findViewById(R.id.btnDriverBack);
btnDriverBack.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent driveBack = new Intent(DriverActivity.this, SummaryActivity.class);
startActivity(driveBack);
}
});
btnDriverSubmit = (Button) findViewById(R.id.btnDriverSubmit);
btnDriverSubmit.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent driverSubmit = new Intent(DriverActivity.this, CreditCardActivity.class);
startActivity(driverSubmit);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1)
{
Bitmap license = (Bitmap) data.getExtras().get("data");
imageDriver.setImageBitmap(license);
}
}
我已设置正确的使用权限。任何帮助将不胜感激,谢谢! :)
答案 0 :(得分:1)
首先尝试这个
private final int REQUEST_CAMERA_USAGE = 200;
if (Build.VERSION.SDK_INT >= 21) {
int permissionCheck = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_USAGE);
} else {
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_CAMERA_USAGE:
if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
} else {
Toast.makeText(getApplicationContext(), "This Permission is needed for the app to work perfectly!", Toast.LENGTH_SHORT).show();
}
default:
Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_SHORT).show();
break;
}
}
答案 1 :(得分:0)
如果你有上面的Lollipop版本,那么添加运行时权限
答案 2 :(得分:0)
要检查是否是权限问题,请转到settings-> your_app->权限并手动将其设置为true,如果问题解决,则在打开相机之前在代码中添加运行时权限。