Android external device devices may or may not be present.
I am writing an application that in a single mp3 stream from a URL and stores it in a file in memory for temporary use.
But I want to be sure that this file is created.
At first I thought about using getCacheDir , but is mentioned:
These files will be those that are deleted first when the device starts low in stock. There is no guarantee that these files will be deleted. Note: you should not rely on a system deleting these files for you; you should always have a reasonable maximum, for example 1 MB, for the amount of space that you consume using cache files, and trim these files when this space is exceeded.
So, I thought about using getExternalCacheDir , but then I'm not sure if the device will contain external memory or not.
There is another additional problem: Before storing the file in memory, I want to check if there is enough space in the directory. How can I do that?
Update
I also thought about creating the file in Environment.getDownloadCacheDirectory(), since it will probably be available on every device.
File f3 = new File(Environment.getDownloadCacheDirectory(), "Temp.txt");
if (!f3.exists()) {
f3.createNewFile();
}
But it turned out below the error trace in createNewFile ():
java.io.IOException: Permission denied
at java.io.File.createNewFileImpl(Native Method)
at java.io.File.createNewFile(File.java:1257)
at com.example.activitylifecycle.MyStreamingApp.startStream(MyStreamingApp.java:42)
at com.example.activitylifecycle.MyStreamingApp.onCreate(MyStreamingApp.java:23)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
at android.app.ActivityThread.access$1500(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:4385)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
at dalvik.system.NativeStart.main(Native Method)
Manifest file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activitylifecycle"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.activitylifecycle.ActivityLifeCycle"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MyStreamingApp"
android:label="@string/app_name" >
<intent-filter android:priority="100" >
<action android:name="com.example.activitylifecycle.MYSTREAMINGAPP" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Please offer. Thank you.