📅  最后修改于: 2023-12-03 15:06:37.418000             🧑  作者: Mango
在开发 Android 应用程序时,许多应用都需要从相机中选择照片。这篇文章介绍了如何实现在 Android 应用中选择照片。对于熟悉 Java 的程序员来说,这篇文章会更容易理解。
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
dependencies {
implementation 'com.github.bumptech.glide:glide:4.11.0'
implementation 'com.github.bumptech.glide:compiler:4.11.0'
}
private static final int REQUEST_IMAGE_CAPTURE = 1;
public void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
ImageView imageView = findViewById(R.id.imageView);
imageView.setImageBitmap(imageBitmap);
}
}
private static final int PICK_IMAGE = 2;
public void pickImage() {
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto, PICK_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_IMAGE && resultCode == RESULT_OK) {
Uri imageUri = data.getData();
ImageView imageView = findViewById(R.id.imageView);
Glide.with(this).load(imageUri).into(imageView);
}
}
选择照片是 Android 应用程序特别是涉及图像处理的应用程序不可或缺的一部分。本篇文章介绍了从相机 android 中选择照片的实现步骤,希望对 Java 程序员有所帮助。