3

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);
CommonKnowledge
  • 769
  • 1
  • 10
  • 35

3 Answers3

8

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

Community
  • 1
  • 1
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
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);
Paolo Rovelli
  • 9,396
  • 2
  • 58
  • 37
2

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);
Dileep P G
  • 571
  • 5
  • 13