从资产或res/raw中的文件中获取Uri [英] Get Uri from file in either assets or res/raw

查看:420
本文介绍了从资产或res/raw中的文件中获取Uri的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使它正常工作,并且我在线上浏览了许多不同的资源(正如您从我所发表的所有评论中所看到的).我想访问位于资产或资源中的.pdf文件;哪一个最简单的方法都无关紧要.

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.

我有下面的方法可以获取实际文件,并使用参数Uri调用另一个方法(在下面的第一个方法下).

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.

非常感谢您的帮助,我将随时为您解答问题或添加更多内容.

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

推荐答案

来自链接& 获取存储在.mp3文件中的URI android中的res/raw文件夹

输入资源ID,格式为:

sing the resource id, the format is:

"android.resource://[package]/[res id]"

Uri路径= 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://[程序包]/[res类型]/[res名称]"

"android.resource://[package]/[res type]/[res name]"

Uri路径= Uri.parse("android.resource://com.androidbook.samplevideo/raw/myvideo");

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

这篇关于从资产或res/raw中的文件中获取Uri的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆