androidstudio设计和完成一个简单的数码相机实现拍照并将拍完的照片显示在页面上并存入相册与读取相册功能并给出完整的Java代码与xml代码
时间: 2024-03-08 07:50:52 浏览: 105
好的,以下是Android Studio实现简单数码相机的Java代码和XML代码:
Java代码:
```java
import android.Manifest;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_IMAGE_CAPTURE = 1;
private static final int REQUEST_PERMISSIONS = 2;
private ImageView mImageView;
private String mImageFileLocation = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = findViewById(R.id.imageView);
Button takePictureButton = findViewById(R.id.button_take_picture);
Button readGalleryButton = findViewById(R.id.button_read_gallery);
takePictureButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (checkPermissions()) {
dispatchTakePictureIntent();
} else {
requestPermissions();
}
}
});
readGalleryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
});
}
private boolean checkPermissions() {
int permissionCamera = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
int permissionStorage = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
return permissionCamera == PackageManager.PERMISSION_GRANTED &&
permissionStorage == PackageManager.PERMISSION_GRANTED;
}
private void requestPermissions() {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_PERMISSIONS);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_PERMISSIONS) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
dispatchTakePictureIntent();
} else {
Toast.makeText(this, "Permissions not granted", Toast.LENGTH_SHORT).show();
}
}
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File imageFile = null;
try {
imageFile = createImageFile();
} catch (IOException e) {
e.printStackTrace();
}
if (imageFile != null) {
Uri imageUri = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
imageFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File imageFile = File.createTempFile(imageFileName, ".jpg", storageDir);
mImageFileLocation = imageFile.getAbsolutePath();
return imageFile;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
if (mImageFileLocation != null && !mImageFileLocation.isEmpty()) {
Bitmap bitmap = BitmapFactory.decodeFile(mImageFileLocation);
mImageView.setImageBitmap(bitmap);
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "My Picture");
values.put(MediaStore.Images.Media.DESCRIPTION, "Photo taken on " + new Date().toString());
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.Images.Media.DATA, mImageFileLocation);
getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
mImageFileLocation = "";
} else if (data != null && data.getData() != null) {
Uri imageUri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
mImageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
```
XML代码:
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:padding="16dp">
<Button
android:id="@+id/button_take_picture"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Take Picture" />
<Button
android:id="@+id/button_read_gallery"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Read Gallery" />
</LinearLayout>
</RelativeLayout>
```
这个简单的数码相机应用程序包含了拍照并将拍完的照片显示在页面上、并存入相册与读取相册功能。在拍照时,应用程序会请求相机和存储权限。拍摄完成后,应用程序会将照片保存到一个临时文件中,并将其插入到相册中。在读取相册时,应用程序会打开系统相册应用程序,用户可以选择要显示的照片。
阅读全文