Android拍照后保存照片
在现代智能手机上,拍照已经成为人们日常生活中不可或缺的功能之一。在Android应用程序中,我们也可以轻松实现拍照功能并保存照片到设备上。本文将介绍如何在Android应用程序中实现拍照后保存照片的功能,同时提供代码示例帮助读者更好地理解。
实现步骤
步骤一:添加权限到AndroidManifest.xml文件中
在AndroidManifest.xml文件中添加相机和存储权限:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
步骤二:创建布局文件
在res/layout目录下创建一个新的布局文件,用于显示拍照按钮和照片预览:
<RelativeLayout xmlns:android="
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btnTakePhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take Photo"
android:layout_centerInParent="true"/>
<ImageView
android:id="@+id/imgPhotoPreview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btnTakePhoto"
android:layout_margin="16dp"
android:visibility="gone"/>
</RelativeLayout>
步骤三:请求相机权限并拍照
在Activity中请求相机权限并处理拍照逻辑:
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CAMERA_PERMISSION = 200;
private static final int REQUEST_IMAGE_CAPTURE = 300;
private Button btnTakePhoto;
private ImageView imgPhotoPreview;
private Uri imageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnTakePhoto = findViewById(R.id.btnTakePhoto);
imgPhotoPreview = findViewById(R.id.imgPhotoPreview);
btnTakePhoto.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},
REQUEST_CAMERA_PERMISSION);
} else {
dispatchTakePictureIntent();
}
}
});
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = createImageFile();
if (photoFile != null) {
imageUri = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
private File createImageFile() {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File imageFile = null;
try {
imageFile = File.createTempFile(imageFileName, ".jpg", storageDir);
} catch (IOException e) {
e.printStackTrace();
}
return imageFile;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
imgPhotoPreview.setVisibility(View.VISIBLE);
imgPhotoPreview.setImageURI(imageUri);
}
}
}
步骤四:处理权限请求结果和保存照片
处理权限请求结果和保存拍摄的照片:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CAMERA_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
dispatchTakePictureIntent();
} else {
Toast.makeText(this, "Camera permission is required to take photos.", Toast.LENGTH_SHORT).show();
}
}
}
总结
通过以上步骤,我们成功实现了在Android应用程序中拍照后保存照片的功能。首先我们添加了相机和存储权限到AndroidManifest.xml文件中,然后创建了布局文件用于显示拍照按钮和照片预