I have an Android (2.2) project in Eclipse (Helios). I want to add an MP3 file to the project so that the MP3 file is deployed to the device along with the application.
I then would like to open the file as a File
object, which means I'd need to know the full path (?) to the file on the device, but I don't know how paths are specified in Android.
Launch Android Studio, and click File > New > Import Project. Locate your project directory, click the build. gradle file you created above to select it, and then click OK to import your project.
select File >New >Import Project. Locate a project you downloaded , select the project's root directory and click OK. Select project from existing sources and click Ok. Your project will be open in the android studio.
Apparently there is a bug in Froyo that prevents WAV playback.
Audio files should be placed in the "res/raw" directory of your project. Then use the id to play it (or attempt to play it)
MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);
mp.start();
Info: http://developer.android.com/guide/topics/media/index.html
Example (mp3): http://www.helloandroid.com/tutorials/musicdroid-audio-player-part-i
Ok, I saw this on the source of another projet, so I didn't really come up with it, but it works.
To add any file to a project, and later be able to use it, you need to put the file (binary, xml, or whatever) on the assets folder of your project.
In this example I will just copy the asset to the filesystem, so I can later access it as any other user file. You can access the assets directly too, take a look at Resources on the documentation.
public void copyfile(String fileName)
{
if (!new File(fileName).exists()){
try
{
InputStream localInputStream = getAssets().open(fileName);
FileOutputStream localFileOutputStream = getBaseContext().openFileOutput(fileName, MODE_PRIVATE);
byte[] arrayOfByte = new byte[1024];
int offset;
while ((offset = localInputStream.read(arrayOfByte))>0)
{
localFileOutputStream.write(arrayOfByte, 0, offset);
}
localFileOutputStream.close();
localInputStream.close();
// The next 3 lines are because I'm copying a binary that I plan
// to execute, so I need it to have execute permission.
StringBuilder command = new StringBuilder("chmod 700 ");
command.append(basedir + "/" + paramString);
Runtime.getRuntime().exec(command.toString());
Log.d(TAG, "File " + paramString + " copied successfully.");
}
catch (IOException localIOException)
{
localIOException.printStackTrace();
return;
}
}
else
Log.d(TAG, "No need to copy file " + paramString);
}
I believe there is probably a better way to copy the file, but this one works, and does not slow down my app even if it's called from onCreate (all files I copy are below 100kb though, so for bigger files, you probably want a separate thread)
Here is how to get the path to your files:
String path = getBaseContext().getFilesDir().getAbsolutePath();
If you want to write the file to another path, say "/sdcard/DCIM/appPictures", I believe you can use this code:
FileOutputStream outFile = FileOutputStream("/sdcard/DCIM/appPictures/" + fileName);
and then copy it byte by byte like in the example above.
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