Get Uri from file in either assets or res/raw

与世无争的帅哥 提交于 2019-12-08 00:46:18

问题


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.

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

回答1:


from link & Get URI of .mp3 file stored in res/raw folder in android

sing the resource id, the format is:

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

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]"

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




回答2:


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.

If, for example, your resource is stored in the /res/raw folder:

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



回答3:


Very Helpful post.

Here's an alternative: Work with a FileDescriptor instead of the Uri, where possible.

Example: (In my case its a raw audio file)

FileDescriptor audioFileDescriptor = this.resources.openRawResourceFd(R.raw.example_audio_file).getFileDescriptor();

this.musicPlayer.setDataSource(backgroundMusicFileDescriptor);


来源:https://stackoverflow.com/questions/11144837/get-uri-from-file-in-either-assets-or-res-raw

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!