在Editactivity中,加载拍照后保存的图片,并提供相应的编辑功能。编辑完成后,将编辑后的图片保存到同一个公共文件夹中,并记录下该图片的路径和其他相关信息(如标题、描述等)。具体代码
时间: 2024-01-22 08:21:11 浏览: 140
首先,你需要在Editactivity的布局文件中添加一个ImageView来显示拍摄的照片,并添加一些编辑控件(例如旋转、裁剪等)。
然后,在Editactivity的Java代码中,你需要做以下几个步骤:
1. 获取从上一个Activity传来的拍摄照片的路径,并将其显示在ImageView中。
```java
Intent intent = getIntent();
String photoPath = intent.getStringExtra("photo_path");
Bitmap bitmap = BitmapFactory.decodeFile(photoPath);
imageView.setImageBitmap(bitmap);
```
2. 添加一些编辑功能,例如旋转和裁剪,其中旋转可以使用Matrix来实现,而裁剪可以使用Bitmap.createBitmap()方法。
```java
// 旋转
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
imageView.setImageBitmap(rotatedBitmap);
// 裁剪
Bitmap croppedBitmap = Bitmap.createBitmap(bitmap, x, y, width, height);
imageView.setImageBitmap(croppedBitmap);
```
3. 将编辑后的图片保存到公共文件夹中,并记录下该图片的路径和其他相关信息。
```java
// 保存图片
String fileName = "edited_" + System.currentTimeMillis() + ".jpg";
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), fileName);
FileOutputStream fos = new FileOutputStream(file);
croppedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
// 记录图片信息
String title = editTextTitle.getText().toString();
String description = editTextDescription.getText().toString();
String editedPhotoPath = file.getAbsolutePath();
// 将图片信息存储到数据库或SharedPreferences中
```
完整的Editactivity代码如下所示:
```java
public class EditActivity extends AppCompatActivity {
private ImageView imageView;
private EditText editTextTitle;
private EditText editTextDescription;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
imageView = findViewById(R.id.imageView);
editTextTitle = findViewById(R.id.editTextTitle);
editTextDescription = findViewById(R.id.editTextDescription);
Intent intent = getIntent();
String photoPath = intent.getStringExtra("photo_path");
Bitmap bitmap = BitmapFactory.decodeFile(photoPath);
imageView.setImageBitmap(bitmap);
findViewById(R.id.buttonRotate).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
imageView.setImageBitmap(rotatedBitmap);
}
});
findViewById(R.id.buttonCrop).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int x = 100;
int y = 100;
int width = 200;
int height = 200;
Bitmap croppedBitmap = Bitmap.createBitmap(bitmap, x, y, width, height);
imageView.setImageBitmap(croppedBitmap);
}
});
findViewById(R.id.buttonSave).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
String fileName = "edited_" + System.currentTimeMillis() + ".jpg";
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), fileName);
FileOutputStream fos = new FileOutputStream(file);
croppedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
String title = editTextTitle.getText().toString();
String description = editTextDescription.getText().toString();
String editedPhotoPath = file.getAbsolutePath();
// 将图片信息存储到数据库或SharedPreferences中
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
```
阅读全文