Select File for MusicNotes

This commit is contained in:
Sebastian 2024-10-15 18:49:28 +02:00
parent fbdb013484
commit 2b7215b61c
6 changed files with 71 additions and 4 deletions

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application <application
android:allowBackup="true" android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules" android:dataExtractionRules="@xml/data_extraction_rules"
@ -23,6 +23,7 @@
<category android:name="android.intent.category.LAUNCHER"/> <category android:name="android.intent.category.LAUNCHER"/>
</intent-filter> </intent-filter>
</activity> </activity>
</application> </application>
</manifest> </manifest>

View File

@ -1,32 +1,71 @@
package core.notevault.ui.home; package core.notevault.ui.home;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.provider.OpenableColumns;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import core.notevault.R;
import core.notevault.databinding.FragmentHomeBinding; import core.notevault.databinding.FragmentHomeBinding;
public class HomeFragment extends Fragment { public class HomeFragment extends Fragment {
private static final int PICK_FILE_REQUEST_CODE = 1;
private FragmentHomeBinding binding; private FragmentHomeBinding binding;
private HomeViewModel homeViewModel;
public View onCreateView(@NonNull LayoutInflater inflater, public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) { ViewGroup container, Bundle savedInstanceState) {
HomeViewModel homeViewModel = homeViewModel =
new ViewModelProvider(this).get(HomeViewModel.class); new ViewModelProvider(this).get(HomeViewModel.class);
binding = FragmentHomeBinding.inflate(inflater, container, false); binding = FragmentHomeBinding.inflate(inflater, container, false);
View root = binding.getRoot(); View root = binding.getRoot();
final TextView textView = binding.textHome; FloatingActionButton importBtn = root.findViewById(R.id.importMusicNotesBtn);
homeViewModel.getText().observe(getViewLifecycleOwner(), textView::setText); importBtn.setOnClickListener(v -> openFileChooser());
return root; return root;
} }
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_FILE_REQUEST_CODE && resultCode == getActivity().RESULT_OK && data != null) {
Uri uri = data.getData();
if (uri != null) {
handleFile(uri);
}
}
}
private void handleFile(Uri uri) {
// Hier kannst du die Logik zum Speichern oder Anzeigen der Datei implementieren
homeViewModel.addNote(uri); // Speichere die URI im ViewModel
Toast.makeText(getActivity(), "Datei ausgewählt: " + uri.getPath(), Toast.LENGTH_SHORT).show();
}
private void openFileChooser() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*"); // Alle Dateitypen
String[] mimeTypes = {"application/pdf", "image/png", "image/jpeg"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, PICK_FILE_REQUEST_CODE);
}
@Override @Override
public void onDestroyView() { public void onDestroyView() {
super.onDestroyView(); super.onDestroyView();

View File

@ -1,5 +1,6 @@
package core.notevault.ui.home; package core.notevault.ui.home;
import android.net.Uri;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel; import androidx.lifecycle.ViewModel;
@ -16,4 +17,8 @@ public class HomeViewModel extends ViewModel {
public LiveData<String> getText() { public LiveData<String> getText() {
return mText; return mText;
} }
public void addNote(Uri uri) {
System.out.println("Added file: " + uri.toString());
}
} }

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:pathData="M440,520L200,520v-80h240v-240h80v240h240v80L520,520v240h-80v-240Z"
android:fillColor="#e8eaed"/>
</vector>

View File

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 -960 960 960" width="24px" fill="#e8eaed"><path d="M440-440H200v-80h240v-240h80v240h240v80H520v240h-80v-240Z"/></svg>

After

Width:  |  Height:  |  Size: 182 B

View File

@ -20,4 +20,16 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/> app:layout_constraintBottom_toBottomOf="parent"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:src="@drawable/add_24dp_e8eaed_fill0_wght400_grad0_opsz24"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:id="@+id/importMusicNotesBtn"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
app:backgroundTint="@color/teal_200"/>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>