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

Com*_*dge 2 java pdf android uri android-resources

我试图让这个工作,我已经在线查看了许多不同的资源(你可以从我所做的所有评论中看到).我想访问位于资产或资源中的.pdf文件; 这对最简单的方法来说无关紧要.

我有下面的方法将获得实际文件,并将参数中的Uri调用另一个方法(在下面的第一个方法下).

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

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);
Run Code Online (Sandbox Code Playgroud)

Dhe*_*ngh 6

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

唱资源ID,格式为:

"android.resource://[package]/[res id]"
Run Code Online (Sandbox Code Playgroud)

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

或者,使用资源子目录(类型)和资源名称(没有扩展名的文件名),格式为:

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

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