请帮忙!我点击它时相机无法打开。我使用的是Android M,也许这就是问题所在。请帮我如何打开相机... 我试图添加一些权限,但它仍然无法正常工作。我已经用过了
但它仍然无法运作。请帮忙
这是我的代码
public class TambahLaporActivity extends AppCompatActivity {
ImageView imageView;
Integer REQUEST_CAMERA = 1, SELECT_FILE = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tambah_lapor);
imageView = (ImageView) findViewById(R.id.image);
CardView imageView = (CardView) findViewById(R.id.cv);
//set a clickListener on that view
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SelectImage();
}
});
}
private void SelectImage() {
final CharSequence[] items = { "Camera", "Gallery", "Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(TambahLaporActivity.this);
builder.setTitle("Add Image");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (items[i].equals("Camera")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
} else if (items[i].equals("Gallery")) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent.createChooser(intent, "Select Type"), SELECT_FILE);
} else if (items[i].equals("Cancel")) {
dialogInterface.dismiss();
}
}
});
builder.show();
}
@Override
public void onActivityResult(int requenstCode, int resultCode, Intent data) {
super.onActivityResult(requenstCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requenstCode == REQUEST_CAMERA) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(bitmap);
} else if (requenstCode == SELECT_FILE) {
Uri selectedImageUri = data.getData();
imageView.setImageURI(selectedImageUri);
}
}
}
}