Get Uri from file in either assets or res/raw

13,270

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

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

Solution 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);
Share:
13,270
CommonKnowledge
Author by

CommonKnowledge

$whoami Nick Hey there! My name is Nick and I have been in the programming macrocosm since 2005. I have a degree in CS and I believe I have a deep-seated understanding of the world thanks to my passion for the outdoors, mathematics, and anything electrical/mechanical. I can do anything, given the chance to learn; similar an alien visitor reverse engineering human technology. Current Languages (as of 2018) PHP Javascript/Java jQuery/PHPExcel/JpGraph/D3.js, yadda yadda, framework, framework, framework Andriod SQL SMX (server macro expansion) HTML/CSS Python Currently I run an I.T. department for a Global Manufacturer, as well as a part-time developer. My Passion is in the code and the overall design of complex systems. I would say I have a gift for seeing the big picture. I have decent communications skills which allow me to interpret complex technical systems to those who are not technically savvy. I have implemented a few systems including a full scale ERP, Parts management system, and a QMS. I am here for the same reason as most, to help and to get help.

Updated on June 04, 2022

Comments

  • CommonKnowledge
    CommonKnowledge almost 2 years

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