0

I know the question has been asked a number of times, but I can't find a suitable solution. But I also think that a new approach is taken with each version. I'm currently trying to create an app that will record a counter and an audio file. This application is intended to be a tool for our reviewers. I am a complete newbie in the field of Android app creation. I am currently failing because I would like to add two or 3 folders to the podcast folder, in which the corresponding audio files are saved. The folder structure should be: / Podcasts / Wind farm name / serial number of the wind turbine. So far I have created the following files for this purpose:

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="de.dirkbrekow.anzeigeundrecorder">

    <uses-permission-sdk-23 android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission-sdk-23 android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission-sdk-23 android:name="android.permission.RECORD_AUDIO"/>

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AnzeigeUndRecorder"
        android:requestLegacyExternalStorage="true">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:layout_marginTop="40dp">

        <com.google.android.material.textfield.TextInputLayout
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp">

            <androidx.appcompat.widget.AppCompatEditText
                android:id="@+id/etName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#e9e9e9"
                android:hint="@string/user_hint"
                android:text="@string/user_name">

            </androidx.appcompat.widget.AppCompatEditText>

        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp">

            <androidx.appcompat.widget.AppCompatEditText
                android:id="@+id/etWindpark"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#e9e9e9"
                android:hint="@string/windpark_hint"
                android:text="@string/windpark_name">

            </androidx.appcompat.widget.AppCompatEditText>

        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp">

            <androidx.appcompat.widget.AppCompatEditText
                android:id="@+id/etAnlage"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#e9e9e9"
                android:hint="@string/wea_hint"
                android:text="@string/wea_name">

            </androidx.appcompat.widget.AppCompatEditText>

        </com.google.android.material.textfield.TextInputLayout>

        <Button
            android:id="@+id/btnStart"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:text="@string/btnStart"
            android:textSize="20sp"
            android:textStyle="bold"/>

    </LinearLayout>



</ScrollView>

MainActivity.kt:

    package de.dirkbrekow.anzeigeundrecorder

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import java.io.File

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val btnStart = findViewById<Button>(R.id.btnStart)
        val etName = findViewById<androidx.appcompat.widget.AppCompatEditText>(R.id.etName)
        val etWindpark = findViewById<androidx.appcompat.widget.AppCompatEditText>(R.id.etWindpark)
        val etAnlage = findViewById<androidx.appcompat.widget.AppCompatEditText>(R.id.etAnlage)

        btnStart.setOnClickListener {
            createFolder()
        }
    }

    private fun createFolder() {
        val workDirectory = File("/Podcasts/Fuchsberg/")
        workDirectory.mkdirs()


    }
}

Thanks in advance

1 Answers1

0

Please try with below method it works for me.

private fun createDirectory(): File {
    val folderName = "/Podcasts/Fuchsberg/"
    var privateTempDir = Environment.getExternalStorageDirectory()
    privateTempDir =
        File(privateTempDir.absolutePath + folderName)
    if (!privateTempDir.exists()) {
        privateTempDir.mkdirs()
    }
    return privateTempDir
}

Please make sure you have the WRITE_EXTERNAL_STORAGE permission.

I hope it's helpful to you!!!

Sanjay Chauhan
  • 893
  • 7
  • 13