如何将文件添加到 Android 项目,将其部署到设备,然后打开它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3663847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a file to an Android project, deploy it to the device, and then open it?
提问by MusiGenesis
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.
我在 Eclipse (Helios) 中有一个 Android (2.2) 项目。我想向项目添加一个 MP3 文件,以便将 MP3 文件与应用程序一起部署到设备。
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.
然后我想将文件作为File
对象打开,这意味着我需要知道设备上文件的完整路径 (?),但我不知道在 Android 中如何指定路径。
回答by Paul Gregtheitroade
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)
显然,Froyo 中存在一个阻止 WAV 播放的错误。
音频文件应放在项目的“res/raw”目录中。然后使用id播放(或尝试播放)
MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);
mp.start();
信息: http://developer.android.com/guide/topics/media/index.htmlhttp://developer.android.com/guide/topics/media/index.html
示例(mp3):http://www.helloandroid.com/tutorials/musicdroid-audio-player-part-ihttp://www.helloandroid.com/tutorials/musicdroid-audio-player-part-i
回答by M Granja
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 assetsfolder of your project.
要将任何文件添加到项目中,然后才能使用它,您需要将文件(二进制文件、xml 或其他文件)放在项目的资产文件夹中。
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 Resourceson 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)
我相信可能有更好的方法来复制文件,但这种方法有效,即使从 onCreate 调用它也不会减慢我的应用程序的速度(尽管我复制的所有文件都低于 100kb,因此对于更大的文件,您可能需要一个单独的线程)
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:
如果要将文件写入另一个路径,请说“/sdcard/DCIM/appPictures”,我相信您可以使用以下代码:
FileOutputStream outFile = FileOutputStream("/sdcard/DCIM/appPictures/" + fileName);
and then copy it byte by byte like in the example above.
然后像上面的例子一样逐字节复制它。