如何在android中获取资产文件夹的文件路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22906068/
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 get file path of asset folder in android
提问by sivanesan1
I have an Android application used to transfer the .png image from asset folder of Android application to my local server. I need to get the path of image in asset folder, pls refer my code below,
我有一个 Android 应用程序,用于将 .png 图像从 Android 应用程序的资产文件夹传输到我的本地服务器。我需要获取资产文件夹中图像的路径,请参考下面的代码,
String stringPath = "android.resource//"+getPackageName()+"/raw/sample/logout.png";
File f = new File(stringPath);
If I use the above code I got "File not found exception". So how to take image path in asset folder?
如果我使用上面的代码,我会得到“找不到文件异常”。那么如何在资产文件夹中获取图像路径?
采纳答案by Nazmul Alam Khan
You could put your mp3 files at : res/raw folder.
您可以将 mp3 文件放在:res/raw 文件夹中。
MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.myringtone);
mediaPlayer.start();
回答by Ion Aalbers
Try:
尝试:
file:///android_asset/raw/sample/logout.png
If you have a Context, you can use context.getAssets().open("raw/sample/logout.png")
to open an InputStream
on the file.
如果您有上下文,则可以使用context.getAssets().open("raw/sample/logout.png")
来打开InputStream
文件。
回答by Sanu
Assets and resources are accessible using file:///android_asset and file:///android_res.
资产和资源可以使用 file:///android_asset 和 file:///android_res 访问。
Uri path = Uri.parse("file:///android_asset/raw/sample/logout.png");
String newPath = path.toString();