我的申请是为了听音乐。我将在互联网上实现mvvm,然后是一个教程。但是,我发现它只用于在sqlite中保存和检索数据。当我删除一个mp3文件时,它只会删除songentity对象(它只是数据库中的一个信息行:标题、大小、艺术家等等)。但是我想删除mp3文件(二进制文件,真实文件,…)在mediastore中,在设备中,。。。你能告诉我解决这个问题的方法吗。我的想法是实现songdao接口,然后在其中编写deletefile()函数,但我不知道如何编写它。
songentity.class类
@Entity(tableName = "song_table")
public class SongEntity implements Song {
public SongEntity() {
Random random = new Random();
songName = String.valueOf(random.nextDouble());
id = random.nextInt();
}
@ColumnInfo(name = "song_name")
private String songName;
@ColumnInfo(name = "song_uri")
private String uriString;
@ColumnInfo(name = "path")
private String path;
@PrimaryKey (autoGenerate = true)
@ColumnInfo(name = "id")
private int id;
@ColumnInfo(name = "size")
private double size;
@ColumnInfo(name = "artist")
private String artist;
@ColumnInfo(name = "singer")
private String singer;
@Override
public void setSongName(String newName) {
songName = newName;
}
@Override
public void setUriString(String newUriString) {
uriString = newUriString;
}
@Override
public String getSongName() {
return songName;
}
@Override
public String getUriString() {
return uriString;
}
@Override
public String getPath() {
return path;
}
@Override
public void setPath(String path) {
this.path = path;
}
@Override
public int getId() {
return id;
}
@Override
public void setId(int id) {
this.id = id;
}
@Override
public double getSize() {
return size;
}
@Override
public void setSize(double size) {
this.size = size;
}
@Override
public String getArtist() {
return artist;
}
@Override
public void setArtist(String artist) {
this.artist = artist;
}
@Override
public String getSinger() {
return singer;
}
@Override
public void setSinger(String singer) {
this.singer = singer;
}
}
松岛级
@刀
public interface SongDAO {
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(SongEntity songEntity);
@Update
void update(SongEntity songEntity);
@Delete
void delete(SongEntity songEntity);
@Query("DELETE FROM song_table")
void deleteAll();
@Query("SELECT * FROM song_table")
LiveData<List<SongEntity>> getAllSongs();
}
数据库.class
public abstract class SongDatabase extends RoomDatabase {
private static SongDatabase instance;
public abstract SongDAO songDAO();
public static synchronized SongDatabase getInstance(Context context) {
if (instance == null) {
instance = Room.databaseBuilder(context,
SongDatabase.class,
"song_database")
.fallbackToDestructiveMigration()
.build();
}
return instance;
}
}
songrepository.class类
public class SongRepository {
private SongDAO songDAO;
private LiveData<List<SongEntity>> allSongs;
public SongRepository(Application application) {
SongDatabase database = SongDatabase.getInstance(application);
songDAO = database.songDAO();
allSongs = songDAO.getAllSongs();
}
public void insert(SongEntity songEntity) {
new InsertSongAsyncTask(songDAO).execute(songEntity);
}
public void update(SongEntity songEntity) {
new UpdateSongAsyncTask(songDAO).execute(songEntity);
}
public void delete(SongEntity song) {
new DeleteSongAsyncTask(songDAO).execute(song);
}
public void deleteAllSongs() {
new DeleteAllSongsAsyncTask(songDAO).execute();
}
public LiveData<List<SongEntity>> getAllSongs() {
return allSongs;
}
private static class InsertSongAsyncTask extends AsyncTask<SongEntity, Void, Void> {
private SongDAO songDAO;
private InsertSongAsyncTask(SongDAO songDAO) {
this.songDAO = songDAO;
}
@Override
protected Void doInBackground(SongEntity... songs) {
songDAO.insert(songs[0]);
return null;
}
}
private static class UpdateSongAsyncTask extends AsyncTask<SongEntity, Void, Void> {
private SongDAO songDAO;
private UpdateSongAsyncTask(SongDAO songDAO) {
this.songDAO = songDAO;
}
@Override
protected Void doInBackground(SongEntity... songs) {
songDAO.update(songs[0]);
return null;
}
}
private static class DeleteSongAsyncTask extends AsyncTask<SongEntity, Void, Void> {
private SongDAO songDAO;
private DeleteSongAsyncTask(SongDAO songDAO) {
this.songDAO = songDAO;
}
@Override
protected Void doInBackground(SongEntity... songs) {
songDAO.delete(songs[0]);
return null;
}
}
private static class DeleteAllSongsAsyncTask extends AsyncTask<Void, Void, Void> {
private SongDAO songDAO;
private DeleteAllSongsAsyncTask(SongDAO songDAO) {
this.songDAO = songDAO;
}
@Override
protected Void doInBackground(Void... voids) {
songDAO.deleteAll();
return null;
}
}
}
以及songviewmodel.class
public class SongViewModel extends AndroidViewModel {
private SongRepository repository;
private LiveData<List<SongEntity>> allSongs;
public SongViewModel(@NonNull Application application) {
super(application);
repository = new SongRepository(application);
allSongs = repository.getAllSongs();
}
public void insert(SongEntity songEntity) {
repository.insert(songEntity);
}
public void update(SongEntity songEntity) {
repository.update(songEntity);
}
public void delete(SongEntity songEntity) {
repository.delete(songEntity);
}
public void deleteAllSongs() {
repository.deleteAllSongs();
}
public LiveData<List<SongEntity>> getAllSongs(){
return allSongs;
}
}
暂无答案!
目前还没有任何答案,快来回答吧!