At the code below is my way to get path of downloads or Music Folder. But At the end it say's that you don't have a permission even though I have. In the Explanation below i explain that how I get error. But my main goal is not just solve that error my main goal is find all music files in android with unity. I searched too much answer in the internet but I could not solve this problem. I can play all musics that i want in windows through this way but it doesn't work on android. I can't understand that in the app info it shows that i gave the permission to all files but how it can be possible that program could not recognize that permission?
In the first scene I'm asking for permissions. After accepting that permissions it automatically redirects me to the next scene which in that scene I'm trying to get Music or downloads folder in device. But it throws an exeption.
This is my manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.your.bundle.identifier" xmlns:tools="http://schemas.android.com/tools" android:versionName="1.0" android:versionCode="1" android:installLocation="preferExternal">
<supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
<application android:theme="@style/UnityThemeSelector" android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="false" android:isGame="true" android:banner="@drawable/app_banner">
<activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="@string/app_name" android:screenOrientation="fullSensor" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
</application>
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="24" />
<uses-feature android:glEsVersion="0x00020000" />
<uses-feature android:name="android.hardware.touchscreen" android:required="false" />
<uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false" />
<uses-feature android:name="android.hardware.touchscreen.multitouch.distinct" android:required="false" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
</manifest>
This is my asking permission code
using System.Collections;
using UnityEngine;
using UnityEngine.Android;
using UnityEngine.SceneManagement;
public class ButtonPressing : MonoBehaviour
{
private bool questionAsked;
private void Start()
{
questionAsked = false;
#if PLATFORM_ANDROID
Permission.RequestUserPermission(Permission.ExternalStorageWrite);
questionAsked = true;
StartCoroutine(MyRoutine());
#endif
}
private IEnumerator MyRoutine()
{
questionAsked = true;
if (questionAsked && Permission.HasUserAuthorizedPermission(Permission.ExternalStorageRead) &&Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite))
{ questionAsked = false;
SceneManager.LoadScene(1);
}
else if (questionAsked && Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite))
{
questionAsked = false;
Permission.RequestUserPermission(Permission.ExternalStorageRead);
}
yield return new WaitForSeconds(3f);
StartCoroutine(MyRoutine());
}
}
And here I'm trying to get downloads or music folder.
[Obsolete("Obsolete")]
public void GetFiles()
{
#if PLATFORM_ANDROID
if (Permission.HasUserAuthorizedPermission(Permission.ExternalStorageRead)&&Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite))
{
_camera.backgroundColor = Color.grey;
_path = "/storage";
directoryTexts.text = _path "\n";
try
{
string[] allDirectories = Directory.GetDirectories(_path, "*Music*", SearchOption.AllDirectories);
directoryTexts.text = allDirectories.Length "\n";
if (allDirectories.Length!=0)
{
foreach (var allDirectory in allDirectories)
{
directoryTexts.text = allDirectory "\n";
}
}
else
{
directoryTexts.text = "There is no directory \n";
}
}
catch (Exception e)
{
directoryTexts.text = e "\n";
throw;
}
}
else
{
directoryTexts.text = "You Dont Have a permission \n";
}
#endif
}
But everytime i get this Exeption Exeption Image
By the way in the image "Yes it has" means yes it has a permission. I have checked it in start function. In Addition I have found in this forum https://forum.unity.com/threads/android-10-and-scoped-storage-missed-features-in-unity-2019.749333/ that others have the same issue.But no one couln'd not solve this problem.
CodePudding user response:
After long researches I found that I have to request ManageExternalStorage permission in order to acces media files in android 11. But in android 10 my code work without any error. Here is Documentation , Documentation1 for java. And with AndroidJavaClass and AndroidJavaObject in Unity you can substitue java code with C# code.
CodePudding user response:
This should find issue.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace SAveDirectoriesXml
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
const string FOLDER = @"c:\temp";
static XmlWriter writer = null;
static void Main(string[] args)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
writer = XmlWriter.Create(FILENAME, settings);
writer.WriteStartDocument(true);
DirectoryInfo info = new DirectoryInfo(FOLDER);
WriteTree(info);
writer.WriteEndDocument();
writer.Flush();
writer.Close();
Console.WriteLine("Enter Return");
Console.ReadLine();
}
static long WriteTree(DirectoryInfo info)
{
long size = 0;
writer.WriteStartElement("Folder");
try
{
writer.WriteAttributeString("name", info.Name);
writer.WriteAttributeString("numberSubFolders", info.GetDirectories().Count().ToString());
writer.WriteAttributeString("numberFiles", info.GetFiles().Count().ToString());
writer.WriteAttributeString("date", info.LastWriteTime.ToString());
foreach (DirectoryInfo childInfo in info.GetDirectories())
{
size = WriteTree(childInfo);
}
}
catch (Exception ex)
{
string errorMsg = string.Format("Exception Folder : {0}, Error : {1}", info.FullName, ex.Message);
Console.WriteLine(errorMsg);
writer.WriteElementString("Error", errorMsg);
}
FileInfo[] fileInfo = null;
try
{
fileInfo = info.GetFiles();
}
catch (Exception ex)
{
string errorMsg = string.Format("Exception FileInfo : {0}, Error : {1}", info.FullName, ex.Message);
Console.WriteLine(errorMsg);
writer.WriteElementString("Error",errorMsg);
}
if (fileInfo != null)
{
foreach (FileInfo finfo in fileInfo)
{
try
{
writer.WriteStartElement("File");
writer.WriteAttributeString("name", finfo.Name);
writer.WriteAttributeString("size", finfo.Length.ToString());
writer.WriteAttributeString("date", info.LastWriteTime.ToString());
writer.WriteEndElement();
size = finfo.Length;
}
catch (Exception ex)
{
string errorMsg = string.Format("Exception File : {0}, Error : {1}", finfo.FullName, ex.Message);
Console.WriteLine(errorMsg);
writer.WriteElementString("Error", errorMsg);
}
}
}
writer.WriteElementString("size", size.ToString());
writer.WriteEndElement();
return size;
}
}
}