[Upgrading, Modifying and Unlocking] gaferreira13:
It's my first relevant post in here so please be benevolent and friendly to me :).

Hello everyone,

After long hours of roaming around on the internet, Moto forums, PowerAMP forums (you wouldn't believe how many people posted this problem in there), huge exchange of emails and debugs to the Nexus Media Importer dev, and checking out many many topics here on XDA, I finally came across the only way to use an USB-OTG pendrive as a music library, and having Android detect it (through the Media Service) and build the music library for other media apps to use.

tl dr; Problem: You wanted loads of music without wasting precious internal memory. You tried using an USB pen but no mp3 apps were able to find the files, nor stickmount, nor whatever.

This is so far the only way to do this on android Marshmallow.​

Believe me, I've tried almost everything one can do, including using the Nexus Media Importer, Stickmount, OTG Helper, etc (spoiler alert, none of these worked).

Why is this happening, you ask? Well, Google changed things on Android 6.x.x so much regarding filesystems and USB support that full read/write on USB OTG with other apps no longer works, although I've never tested on previous versions of Android. But I finally managed to find a way to do it, and here I intend to share it with you guys.

Requirements - Rooted phone and permissive SELinux (what is that and how do I do it? don't worry, it's easy and I'll explain a bit later)

1st step

- Verify that your phone is capable of using USB-OTG. If it is not, check on play store for other apps that supposedly can enable USB Host support on your phone, although I've never tried them because I never needed, so I can't say anything. My phone is a Moto X 2014 and it comes with OTG support embedded.

2nd step

- Format your pendrive as NTFS. This is

very important

because...

If you use your pendrive as Fat32, Android will mount it to a folder in /data/media_rw/blablabla with GID and UID 1023 (media_rw), which most apps don't have permissions to access, thus they won't be able to find your pendrive and build a music library. Why? Well, glad you asked...

...Since Fat32 doesn't support the linux permissions system, Android mounts the pendrive with the FUSE fs wrapper, which cloaks a permission system hard-coded into some file in the Android system that I wasn't able to tell how could I modify it in order to set the pen permissions as 777 root root.

You can do so by plugging your pen on a Windows PC and right clicking -> Format. Remember to

backup your data

, otherwise you will lose everything. Your pen will still be able to be used on Windows pcs and Macs. You could also use other fileformats...

... like EXT4 or f2fs or exFAT or whatever, but do it at your own risk. I have no idea if those would work, despite them being able to use the linux permission system.

3rd step

- Install the wonderful app Paragon HFS and NTFS mounter from appstore or whatever. They say it isn't compatible with marshmallow but it is, the only issue is that it can't mount a pendrive automatically but it's very easy to do it manually. In the settings menu, you can set SELinux as permissive. Since I'm not an android dev, only an ambicious amateur, I can't really explain what it does or what happens when you do that. So far, neither me or my phone died because of that, so. ;)

Oh by the way, whenever you insert the pen , Android will say it's corrupted and ask you to format it.

DO NOT DO IT!

That happens because Android doesn't natively support NTFS (hence using an external app to solve that). Oh, and since we're talking about that, there is a way for Android to natively support NTFS...

..., just search it up on XDA. Beware that it's outdated and it won't work on Marshmallow either, unless you edit the copymodulecrc script, but that goes beyond my knowledge. For now, stick to the plan.

4th step

- You need to choose the folder which you want to mount the pen to, which will be a folder in the root Android folder (I chose /pen so that I wouldn't forget)

You are now probably thinking, "That's insane! You need to edit init.rc in order to do that!" Well, you're kinda right, but there is a workaround for that. Why do I do this? Well...

... if you mount the pen in another random folder such as /storage/emulated/0/pen it won't work because all the folders underlying that one have certain permission issues that don't let other media apps be able to search for music files.

So here I'll show you two ways of accomplishing this.

4.1 Editing init.rc - This is very complex and

NOT RECOMMENDED

unless you're very comfortable with messing with boot files.

PLEASE DO NOT DO THIS

if you aren't sure of what you're doing, don't tell me I didn't warn you.

So in order to do this, well, I won't explain it to you. Search it up on the internet, it's very complex and too big for me to post here. But in the end what you want to do is edit the init.rc file, adding two lines:

"mkdir /pen" and "chmod 777 /pen".

4.2 Creating a script that runs on boot - This is the one method I recommend since it's harmless, but you have to install yet another app, that allows you to run scripts at boot. I used script manager. Then create a script that mounts the / folder as rw, creates the folder /pen, chmods it to 777. I used the following:

#!/system/bin/sh

su -c mount -o rw,remount /proc /

su -c mkdir /pen

su -c chmod 777 /pen

I tried other mount -o commands with rootfs instead of /proc but they didn't work. I don't know the difference but I hope it's harmless. Anyways, let's move on.

Run the script to see if it works, and to create the folder. Use a rooted file explorer to verify if the folder was created.

5th step

- Reboot your phone if you had to set SELinux as permissive, and type on the Terminal "su -c getenforce" to confirm that your SELinux is permissive.

VERY IMPORTANT

- On the Paragon app, open settings and disable FS access rights, otherwise it's the same as mounting a Fat32 pendrive - not what we want.

On the Paragon app, choose the folder we created before as the folder to mount the pen to, and mount the pendrive. If you followed all the previous steps correctly, the app won't have any issues mounting the pen.

6th step

- Find a way to enforce Android building a media library with the new files in the USB OTG pendrive. There are many ways to do this and I recomment the one I used, but I will tell you other ways.

I used the app jetAudio which is a media player that can also forceably build a media library. On jetAudio settings menu, press "Rebuild media library" and point to the /pen folder. It will start reading all the files and Android now detects your pendrive as a media library.

Another way would be to use an app called FolderMount, and creat a symlink from the /pen to another folder in your emulated SD storage (you know, the only folder you have access to if you're not rooted), and wait for some time until hopefully Android detects your new music. If you don't want to use FolderMount, do a symlink with another method (like the ln command)

EDIT - Another cool way I found elsewhere on StackExchange is to run this command (through a script or whatever):

am broadcast -a android.intent.action.MEDIA_MOUNTED -n com.android.providers.media/.MediaScannerReceiver (-d file:///pen)

which forces Android to run a media scan. The part on parenthesis is optional, you can use it to choose the folder in which Android runs the media scan.

Hurray! Congrats for reaching this part of the post. If you did everything correctly, all media apps will finally be able to find the files. I posted some screenies to orientate you.

Feel free to ask anything and I will try to answer or explain. Remember that I'm no Android dev so my knowledge is limited. Feel free to thank me, if you want too.