安卓将拍照的照片存入Room数据库,并且调用照片路径有几种方法,具体示例
时间: 2023-12-10 20:41:00 浏览: 184
Android 图片存储到指定路径和相册的方法
将拍照的照片存入Room数据库,可以按照以下步骤进行操作:
1. 创建一个实体类 Photo,定义照片的属性,如名称、路径、拍摄时间等:
```
@Entity(tableName = "photo_table")
public class Photo {
@PrimaryKey(autoGenerate = true)
private int id;
@ColumnInfo(name = "name")
private String name;
@ColumnInfo(name = "path")
private String path;
@ColumnInfo(name = "time")
private long time;
// getters and setters
}
```
2. 创建一个 Dao 接口 PhotoDao,定义插入、查询等操作:
```
@Dao
public interface PhotoDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(Photo photo);
@Query("SELECT * FROM photo_table")
List<Photo> getAllPhotos();
// 其他操作
}
```
3. 创建一个 Room 数据库 PhotoDatabase,定义数据库的版本、实体类和 Dao 接口:
```
@Database(entities = {Photo.class}, version = 1)
public abstract class PhotoDatabase extends RoomDatabase {
public abstract PhotoDao photoDao();
}
```
4. 在应用程序中,创建一个 ViewModel 类 PhotoViewModel,调用 PhotoDao 中的方法进行数据库操作:
```
public class PhotoViewModel extends AndroidViewModel {
private PhotoDao photoDao;
private LiveData<List<Photo>> allPhotos;
public PhotoViewModel(@NonNull Application application) {
super(application);
PhotoDatabase database = PhotoDatabase.getInstance(application);
photoDao = database.photoDao();
allPhotos = photoDao.getAllPhotos();
}
public void insert(Photo photo) {
new InsertAsyncTask(photoDao).execute(photo);
}
public LiveData<List<Photo>> getAllPhotos() {
return allPhotos;
}
private static class InsertAsyncTask extends AsyncTask<Photo, Void, Void> {
private PhotoDao photoDao;
private InsertAsyncTask(PhotoDao photoDao) {
this.photoDao = photoDao;
}
@Override
protected Void doInBackground(Photo... photos) {
photoDao.insert(photos[0]);
return null;
}
}
}
```
5. 在拍照的 Activity 中,获取照片的路径并插入到数据库中:
```
public class CameraActivity extends AppCompatActivity {
private PhotoViewModel photoViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
photoViewModel = new ViewModelProvider(this).get(PhotoViewModel.class);
// 调用相机拍照,并获取照片的路径
// ...
Photo photo = new Photo();
photo.setName("照片名称");
photo.setPath(photoPath);
photo.setTime(System.currentTimeMillis());
photoViewModel.insert(photo);
}
}
```
调用照片路径有多种方法,以下是其中两种具体示例:
1. 将照片路径存储到 SharedPreferences 中,然后在其他 Activity 中从 SharedPreferences 中获取路径:
```
// 存储照片路径到 SharedPreferences 中
SharedPreferences.Editor editor = getSharedPreferences("photo", MODE_PRIVATE).edit();
editor.putString("path", photoPath);
editor.apply();
// 从 SharedPreferences 中获取照片路径
SharedPreferences preferences = getSharedPreferences("photo", MODE_PRIVATE);
String path = preferences.getString("path", "");
```
2. 使用 startActivityForResult 方法打开相机,然后在 onActivityResult 回调方法中获取照片路径:
```
// 在 Activity 中打开相机
private static final int REQUEST_CODE_CAMERA = 1;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CODE_CAMERA);
// 在 onActivityResult 方法中获取照片路径
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_CAMERA && resultCode == RESULT_OK && data != null) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap bitmap = (Bitmap) extras.get("data");
String photoPath = saveBitmap(bitmap);
}
}
}
// 保存照片到本地,并返回照片路径
public String saveBitmap(Bitmap bitmap) {
File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), System.currentTimeMillis() + ".jpg");
try {
FileOutputStream outputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return file.getAbsolutePath();
}
```
阅读全文