1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| public class MainActivity extends AppCompatActivity{ private ImageView imageView; private final int CAMERA = 1; private final int PICTURE = 2; private final int REQUEST = 3;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); requestPermission(); }
private void init() { imageView = findViewById(R.id.imageView); }
private void requestPermission() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED && ActivityCompat.checkSelfPermission(this,Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED && ActivityCompat.checkSelfPermission(this,Manifest.permission.CAMERA) == PackageManager.PERMISSION_DENIED){ ActivityCompat.requestPermissions(this,new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA },REQUEST); } }
public void camera(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent,CAMERA); }
public void picture(View view) {
Intent intent = new Intent(Intent.ACTION_PICK,null); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*"); startActivityForResult(intent,PICTURE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == CAMERA){
if (data == null || data.getExtras() == null){ Toast.makeText(this,"没有拍摄照片",Toast.LENGTH_SHORT).show(); } else { Bitmap bitmap = (Bitmap) data.getExtras().get("data"); imageView.setImageBitmap(bitmap); } }
else if (requestCode == PICTURE){
if (data == null){ Toast.makeText(this,"没有选择图片",Toast.LENGTH_SHORT).show(); } else { Uri uri = data.getData(); imageView.setImageURI(uri); } } }
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == REQUEST){ if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED && ActivityCompat.checkSelfPermission(this,Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED){ Toast.makeText(this,"外部储存读写权限申请失败",Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this,"外部储存读写权限申请成功",Toast.LENGTH_SHORT).show(); }
if (ActivityCompat.checkSelfPermission(this,Manifest.permission.CAMERA) == PackageManager.PERMISSION_DENIED){ Toast.makeText(this,"相机权限申请失败",Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this,"相机权限申请成功",Toast.LENGTH_SHORT).show(); } } } }
|