java 从资产或 res/raw 中的文件中获取 Uri
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11144837/
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
Get Uri from file in either assets or res/raw
提问by CommonKnowledge
I have tried to get this working and I have looked at many different resources online (as you can see from all of the comments I have made). I want to access a .pdf file that is either located in assets or res; It does not matter to which one so the easiest way will do.
我试图让它发挥作用,并且我在网上查看了许多不同的资源(正如您从我所做的所有评论中看到的那样)。我想访问位于 assets 或 res 中的 .pdf 文件;选择哪一个并不重要,所以最简单的方法就可以了。
I have the method below that will get the actual file and will call another method(under the first method below) with the Uri in the parameters.
我有下面的方法,它将获取实际文件,并使用参数中的 Uri 调用另一个方法(在下面的第一个方法下)。
Thank you very much for your help and I will be standing by to answer questions or add more content.
非常感谢您的帮助,我将随时待命回答问题或添加更多内容。
private void showDocument(File file)
{
//////////// ORIGINAL ////////////////////
//showDocument(Uri.fromFile(file));
//////////////////////////////////////////
// try 1
//File file = new File("file:///android_asset/RELATIVEPATH");
// try 2
//Resources resources = this.getResources();
// try 4
String PLACEHOLDER= "file:///android_asset/example.pdf";
File f = new File(PLACEHOLDER);
//File f = new File("android.resource://res/raw/slides1/example.pdf");
//getResources().openRawResource(R.raw.example);
// try 3
//Resources resources = this.getResources();
//showDocument(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + resources.getResourcePackageName(R.raw.example) + '/' + resources.getResourceTypeName(R.raw.example) + '/' + resources.getResourceEntryName(R.raw.example)));
showDocument(Uri.fromFile(f));
}
protected abstract void showDocument(Uri uri);
回答by Dheeresh Singh
from link& Get URI of .mp3 file stored in res/raw folder in android
从链接并获取存储在 android res/raw 文件夹中的 .mp3 文件的 URI
sing the resource id, the format is:
sing资源id,格式为:
"android.resource://[package]/[res id]"
Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/" + R.raw.myvideo);
Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/" + R.raw.myvideo);
or, using the resource subdirectory (type) and resource name (filename without extension), the format is:
或者,使用资源子目录(类型)和资源名称(不带扩展名的文件名),格式为:
"android.resource://[package]/[res type]/[res name]"
“android.resource://[包]/[资源类型]/[资源名称]”
Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/raw/myvideo");
Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/raw/myvideo");
回答by Paolo Rovelli
If you do not know the ID of your resource, but just the name, you can use the getIdentifier(...)method of the Android Resouces object. You can retrieve the latter using the getResources()of your application context.
如果您不知道资源的 ID,而只知道名称,则可以使用Android Resouces 对象的getIdentifier(...)方法。您可以使用应用程序上下文的getResources()检索后者。
If, for example, your resource is stored in the /res/rawfolder:
例如,如果您的资源存储在/res/raw文件夹中:
String rawFileName = "example" // your file name (e.g. "example.pdf") without the extension
//Retrieve the resource ID:
int resID = context.getResources().getIdentifier(rawFileName, "raw", context.getPackageName());
if ( resID == 0 ) { // the resource file does NOT exist!!
//Debug:
Log.d(TAG, rawFileName + " DOES NOT EXISTS! :(\n");
return;
}
//Read the resource:
InputStream inputStream = context.getResources().openRawResource(resID);
回答by Dileep P G
Very Helpful post.
非常有帮助的帖子。
Here's an alternative: Work with a FileDescriptor instead of the Uri, where possible.
这是一个替代方案:在可能的情况下使用 FileDescriptor 而不是 Uri。
Example: (In my case its a raw audio file)
示例:(在我的情况下,它是一个原始音频文件)
FileDescriptor audioFileDescriptor = this.resources.openRawResourceFd(R.raw.example_audio_file).getFileDescriptor();
this.musicPlayer.setDataSource(backgroundMusicFileDescriptor);