简体   繁体   English

从资产或res / raw中获取Uri文件

[英]Get Uri from file in either assets or res/raw

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; 我想访问位于资产或资源中的.pdf文件; It does not matter to which one so the easiest way will do. 这对最简单的方法来说无关紧要。

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);

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: 唱资源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:// [package] / [res type] / [res name]”

Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/raw/myvideo"); Uri path = Uri.parse(“android.resource://com.androidbook.samplevideo/raw/myvideo”);

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 . 如果您不知道资源的ID,而只知道名称,则可以使用Android Resouces对象getIdentifier(...)方法。 You can retrieve the latter using the getResources() of your application context. 您可以使用应用程序上下文的getResources()检索后者。

If, for example, your resource is stored in the /res/raw folder: 例如,如果您的资源存储在/ 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);

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM