怎么将相机拍出的照片传送到编辑页面,编辑页面照片和文本框,点击save按钮后保存到Room数据库,点击cancel按钮,返回相机拍照界面,并且删除该照片,完整示例
时间: 2023-10-03 08:02:18 浏览: 151
首先,我们需要在相机拍照界面上添加一个按钮,用于跳转到编辑页面,并将拍摄的照片传递到编辑页面。可以使用以下代码:
```xml
<Button
android:id="@+id/edit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit"
android:onClick="editPhoto" />
```
在 `MainActivity` 中,编写 `editPhoto` 方法,用于跳转到编辑页面并传递照片路径:
```java
public void editPhoto(View view) {
Intent intent = new Intent(this, EditActivity.class);
intent.putExtra("photoPath", currentPhotoPath);
startActivityForResult(intent, EDIT_REQUEST_CODE);
}
```
这里使用 `startActivityForResult` 方法来启动编辑页面,并在返回时获取编辑结果。需要在 `onActivityResult` 方法中处理编辑结果:
```java
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == EDIT_REQUEST_CODE && resultCode == RESULT_OK) {
// 获取编辑后的结果
String text = data.getStringExtra("text");
String photoPath = data.getStringExtra("photoPath");
// 保存到数据库
saveToDatabase(text, photoPath);
// 删除照片
File file = new File(photoPath);
file.delete();
// 返回相机拍照界面
dispatchTakePictureIntent();
}
}
```
在编辑页面中,我们需要显示照片和文本框,并在点击保存按钮时将编辑结果返回到 `MainActivity`。可以使用以下代码:
```xml
<ImageView
android:id="@+id/photo_image_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/placeholder" />
<EditText
android:id="@+id/text_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your text here" />
<Button
android:id="@+id/save_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:onClick="savePhoto" />
<Button
android:id="@+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:onClick="cancelPhoto" />
```
在 `EditActivity` 中,编写 `savePhoto` 方法,用于返回编辑结果。需要使用 `setResult` 方法设置返回结果,并在点击保存按钮时关闭当前页面:
```java
public void savePhoto(View view) {
String text = editText.getText().toString();
Intent intent = new Intent();
intent.putExtra("text", text);
intent.putExtra("photoPath", photoPath);
setResult(RESULT_OK, intent);
finish();
}
```
在 `EditActivity` 中,编写 `cancelPhoto` 方法,用于返回相机拍照界面并删除照片。需要使用 `setResult` 方法设置返回结果,并在点击取消按钮时关闭当前页面:
```java
public void cancelPhoto(View view) {
File file = new File(photoPath);
file.delete();
setResult(RESULT_CANCELED);
finish();
}
```
最后,在 `MainActivity` 中,编写 `saveToDatabase` 方法,用于将照片和文本保存到 Room 数据库:
```java
private void saveToDatabase(String text, String photoPath) {
PhotoDao photoDao = AppDatabase.getInstance(this).photoDao();
Photo photo = new Photo(text, photoPath);
photoDao.insert(photo);
}
```
完整代码示例:
`MainActivity.java`
```java
public class MainActivity extends AppCompatActivity {
static final int REQUEST_IMAGE_CAPTURE = 1;
static final int EDIT_REQUEST_CODE = 2;
String currentPhotoPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
ex.printStackTrace();
}
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = image.getAbsolutePath();
return image;
}
public void editPhoto(View view) {
Intent intent = new Intent(this, EditActivity.class);
intent.putExtra("photoPath", currentPhotoPath);
startActivityForResult(intent, EDIT_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
// Show the thumbnail on ImageView
ImageView imageView = findViewById(R.id.photo_image_view);
Bitmap bitmap = BitmapFactory.decodeFile(currentPhotoPath);
imageView.setImageBitmap(bitmap);
} else if (requestCode == EDIT_REQUEST_CODE && resultCode == RESULT_OK) {
// Get the edited text and photo path
String text = data.getStringExtra("text");
String photoPath = data.getStringExtra("photoPath");
// Save to database
saveToDatabase(text, photoPath);
// Delete the photo
File file = new File(photoPath);
file.delete();
// Return to camera view
dispatchTakePictureIntent();
}
}
private void saveToDatabase(String text, String photoPath) {
PhotoDao photoDao = AppDatabase.getInstance(this).photoDao();
Photo photo = new Photo(text, photoPath);
photoDao.insert(photo);
}
}
```
`EditActivity.java`
```java
public class EditActivity extends AppCompatActivity {
EditText editText;
String photoPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
editText = findViewById(R.id.text_edit_text);
Intent intent = getIntent();
photoPath = intent.getStringExtra("photoPath");
ImageView imageView = findViewById(R.id.photo_image_view);
Bitmap bitmap = BitmapFactory.decodeFile(photoPath);
imageView.setImageBitmap(bitmap);
}
public void savePhoto(View view) {
String text = editText.getText().toString();
Intent intent = new Intent();
intent.putExtra("text", text);
intent.putExtra("photoPath", photoPath);
setResult(RESULT_OK, intent);
finish();
}
public void cancelPhoto(View view) {
File file = new File(photoPath);
file.delete();
setResult(RESULT_CANCELED);
finish();
}
}
```
`Photo.java`
```java
@Entity(tableName = "photo")
public class Photo {
@PrimaryKey(autoGenerate = true)
public int id;
public String text;
public String photoPath;
public Photo(String text, String photoPath) {
this.text = text;
this.photoPath = photoPath;
}
}
```
`AppDatabase.java`
```java
@Database(entities = {Photo.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase instance;
public static synchronized AppDatabase getInstance(Context context) {
if (instance == null) {
instance = Room.databaseBuilder(context.getApplicationContext(),
AppDatabase.class, "app_database")
.build();
}
return instance;
}
public abstract PhotoDao photoDao();
}
```
`PhotoDao.java`
```java
@Dao
public interface PhotoDao {
@Insert
void insert(Photo photo);
@Query("SELECT * FROM photo")
List<Photo> getAll();
@Delete
void delete(Photo photo);
@Query("DELETE FROM photo")
void deleteAll();
}
```
阅读全文