Java教程

【Java转Android】35,涨姿势

本文主要是介绍【Java转Android】35,涨姿势,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

private ImageView picture;

private Uri imageUri;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button takePhoto = findViewById(R.id.take_photo);

Button chooseFromAlbum = findViewById(R.id.choose_from_album);

picture = findViewById(R.id.picture);

takePhoto.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions(MainActivity.this, new String[]{ Manifest.permission.CAMERA }, 2);

}else {

openCamera();

}

}

});

chooseFromAlbum.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions(MainActivity.this, new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE }, 1);

} else {

openAlbum();

}

}

});

}

private void openAlbum() {

Intent intent = new Intent(“android.intent.action.GET_CONTENT”);

intent.setType(“image/*”);

startActivityForResult(intent, CHOOSE_PHOTO); // 打开相册

}

private void openCamera(){

// 创建File对象,用于存储拍照后的图片

File outputImage = new File(getExternalCacheDir(), “output_image.jpg”);

try {

if (outputImage.exists()) {

outputImage.delete();

}

outputImage.createNewFile();

} catch (IOException e) {

e.printStackTrace();

}

if (Build.VERSION.SDK_INT < 24) {

imageUri = Uri.fromFile(outputImage);

} else {

imageUri = FileProvider.getUriForFile(MainActivity.this, “aystudio.nopi.testapp.fileprovider”, outputImage);

}

// 启动相机程序

Intent intent = new Intent(“android.media.action.IMAGE_CAPTURE”);

intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

startActivityForResult(intent, TAKE_PHOTO);

}

@Override

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

switch (requestCode) {

case 1:

if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

openAlbum();

} else {

Toast.makeText(this, “You denied the permission”, Toast.LENGTH_SHORT).show();

}

break;

case 2:

if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

openCamera();

} else {

Toast.makeText(this, “You denied the permission”, Toast.LENGTH_SHORT).show();

}

break;

default:

}

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

switch (requestCode) {

case TAKE_PHOTO:

if (resultCode == RESULT_OK) {

try {

// 将拍摄的照片显示出来

Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));

picture.setImageBitmap(bitmap);

} catch (Exception e) {

e.printStackTrace();

}

}

break;

case CHOOSE_PHOTO:

if (resultCode == RESULT_OK) {

// 判断手机系统版本号

if (Build.VERSION.SDK_INT >= 19) {

// 4.4及以上系统使用这个方法处理图片

handleImageOnKitKat(data);

} else {

// 4.4以下系统使用这个方法处理图片

handleImageBeforeKitKat(data);

}

}

break;

default:

break;

}

}

@TargetApi(19)

private void handleImageOnKitKat(Intent data) {

String imagePath = null;

Uri uri = data.getData();

Log.d(“TAG”, "handleImageOnKitKat: uri is " + uri);

if (DocumentsContract.isDocumentUri(this, uri)) {

// 如果是document类型的Uri,则通过document id处理

String docId = DocumentsContract.getDocumentId(uri);

if(“com.android.providers.media.documents”.equals(uri.getAuthority())) {

String id = docId.split("

这篇关于【Java转Android】35,涨姿势的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!