In android devices external memory may or may not be present.
I am writing an app which in one case stream mp3 from URL & stores it in a file in memory for temporary purpose.
But I want to be sure that, that file will get created.
First I thought of using getCacheDir , but it is mentioned:
These files will be ones that get deleted first when the device runs low on storage. There is no guarantee when these files will be deleted. Note: you should not rely on the system deleting these files for you; you should always have a reasonable maximum, such as 1 MB, for the amount of space you consume with cache files, and prune those files when exceeding that space.
So, then I thought of using getExternalCacheDir , but then I'm not sure that the device will contain external memory or not.
There is one more optional issue: Before saving the file in memory, I want to check if the directory has enough space. How can this be done?
Update
I also thought of creating a file in Environment.getDownloadCacheDirectory()
, as probably it will be available on every device.
File f3 = new File(Environment.getDownloadCacheDirectory(), "Temp.txt");
if (!f3.exists()) {
f3.createNewFile();
}
But got below 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 suggest. Thank You
you write the file in /data/data/. your-package-name is the same name mentioned in AndroidManifest.xml file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With