I'm having a problem getting files from my Android application's cache folder. This is what I'm trying to to but doesn't work:
File cacheDir = getApplicationContext().getCacheDir();
myFile = File.createTempFile("filename", ".mp3", cacheDir);
...
// Here I have to code to initialize the file
...
Log.i(LOG_TAG, "file.length = "+myFile.length()); // This logs correct info
...
//File file = new File(myFile.getPath());
//Log.i(LOG_TAG, "file.length() = "+file.length()); // This logs 0
//Log.i(LOG_TAG, "file.name = "+file.getName()); // This is correct
String fileURI = myFile.getPath();
mediaPlayer.setDataSource(fileURI);
mediaPlayer.prepare(); // This crashes
As you can see it seems the file doesn't contain anything. However I've tried to swap the two first lines with this piece of code and then it works.
myFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MYFOLDER/filename.mp3");
I haven't found any way to set my mediaPlayer without the URI and when I try to get the file from the URI it seems to be a problem.
Do you have solution to how I can get the file from the cache directory?