Copy Image of notes to interal app storage

This commit is contained in:
sebastian 2024-10-31 20:31:58 +01:00
parent 3088f5e7b0
commit f5291e290f

View File

@ -19,6 +19,9 @@ import core.notevault.data.MusicNote;
import core.notevault.databinding.ActivityMainBinding;
import core.notevault.ui.metadatadialog.MetaDataDialog;
import java.io.*;
import java.nio.file.Files;
public class MainActivity extends AppCompatActivity implements MetaDataDialog.OnMetadataListener {
private AppBarConfiguration mAppBarConfiguration;
@ -65,16 +68,31 @@ public class MainActivity extends AppCompatActivity implements MetaDataDialog.On
@Override
public void onMetadataEntered(Uri uri, String title, String composer, int year, String genre) {
MusicNote musicNote = new MusicNote(title, uri.toString(), composer, year, genre);
String path = saveImageInternally(uri);
MusicNote musicNote = new MusicNote(title, path, composer, year, genre);
// Anfordern der dauerhaften Lese-/Schreibberechtigungen für die URI
getContentResolver().takePersistableUriPermission(
uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
);
new Thread(() -> {
musicDB.musicNoteDao().insert(musicNote);
}).start();
}
// Speichere eine Kopie des Bilds im internen Speicher
private String saveImageInternally(Uri uri) {
try (InputStream inputStream = getContentResolver().openInputStream(uri)) {
File imageFile = new File(getFilesDir(), "saved_image_" + System.currentTimeMillis() + ".jpg");
try (OutputStream outputStream = Files.newOutputStream(imageFile.toPath())) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
}
return imageFile.getAbsolutePath(); // Pfad speichern, um später darauf zuzugreifen
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}