Why is USB Storage being detected only in some android devices and not all?
I am working on a sample project in unity android where application creates a text in an OTG pen drive connected to the android phone, the problem is my code works fine in some devices but it doesn't work in some devices. I am unable to figure out the main cause even through the logcat extreme as it doesn't throw any error at all that specifies code error.
I tried adding text logs to print text in between the code to see which texts are not printed, which would further tell me that after which line code stopped working but all the texts are printed.
This is my code that detects the USB and returns true if a text file is present in USB:
public bool LocateFile()
{
textfileData = null;
using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
using (AndroidJavaObject context = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
{
// Get all available external file directories (emulated and USBdrives)
AndroidJavaObject[] externalFilesDirectories = context.Call<AndroidJavaObject[]>("getExternalFilesDirs", (object)null);
AndroidJavaObject emulated = null;
AndroidJavaObject USBdrive = null;
for (int i = 0; i < externalFilesDirectories.Length; i++)
{
AndroidJavaObject directory = externalFilesDirectories[i];
using (AndroidJavaClass environment = new AndroidJavaClass("android.os.Environment"))
{
bool isRemovable = environment.CallStatic<bool>("isExternalStorageRemovable", directory);
bool isEmulated = environment.CallStatic<bool>("isExternalStorageEmulated", directory);
if (isEmulated)
{
emulated = directory;
}
else if (isRemovable && isEmulated == false)
{
USBdrive = directory;
}
}
}
// Return the USBdrive if available
if (USBdrive != null)
{
if (File.Exists(USBdrive.Call<string>("getAbsolutePath") + "/serial.txt"))
{
textfileData = File.ReadAllText(USBdrive.Call<string>("getAbsolutePath") + "/serial.txt");
return true;
}
}
}
}
}
Update: I edited my code to print a text of length of GetExternalFilesDir and it only reads 1 i.e. internal storage. How do I get this to detect external storage as well?
Answer by destro22 · Feb 12, 2020 at 10:21 AM
If there is a problem with my question at least let me know so I can do something about it. How can you leave a developer ignored like that?
Answer by Ikaro88 · Feb 13, 2020 at 02:06 PM
did you solve the problem? with your code my usb is detected but i can not read the file (but it get if exist or not)
No I haven't. $$anonymous$$ake sure to test it in multiple devices. To read file save the location then locate file in that location.
Your answer
Follow this Question
Related Questions
Android getUuidForPath of shared/external storage volumes 0 Answers
How to load android activity when the button clicked in unity scene? 0 Answers
How can I use SQLite Database made with android studio in Unity? 0 Answers
implementing android activity? 0 Answers
How turn On/Off android display backlight using C# only in Unity3d 1 Answer