Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to copy my db file to internal storage root/document. My codes are working good till Android 9 but after it i am getting the error "System.UnauthorizedAccessException: Access to the path "/storage/emulated/0/Documents/FruitsApp/Backup/Fruits.db_2021-06-28 12:20:20" is denied" I have try lots of way to copy all are working before Android 9 but after it i am getting above error. I am sharing my all codes. Thanks in advance.


What I have tried:

Java.IO.File mediaStorageDir = new Java.IO.File(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDocuments) + path);            
            
            if (!mediaStorageDir.Exists())
            {
                var tt = mediaStorageDir.Mkdirs();

                if (mediaStorageDir.Mkdirs() == false)
                {

                    var fail = "failed to create";

                }
            }

       
            var directoryPath = mediaStorageDir.AbsolutePath;


          ////////--this way to create folder is working till andorid 9
           
           //var PathExists = Directory.Exists(directoryPath);

            //if (PathExists ==false)
            //{
            //    Directory.CreateDirectory(directoryPath);
            //}


            var dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "Fruits.db");


            FileInfo file = new FileInfo(dbPath);

            var dbName = file.Name;

            var fullFileName = string.Concat("/", dbName + "_", Global.CurrentDateTime());

            var newpath = string.Format("{0}{1}", directoryPath, fullFileName);
              
              //////--- First way copy file from source to destination is working tille android 9
              
            //using (FileStream sourceStream = System.IO.File.Open(dbPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            //using (FileStream destinationStream = System.IO.File.Create(newpath))
            //{
            //     sourceStream.CopyToAsync(destinationStream);
            //}

         //////--- 2nd way copy file from source to destination is working tille android 9
            
            byte[] dbFile = System.IO.File.ReadAllBytes(dbPath);
            System.IO.File.WriteAllBytes(newpath, dbFile);

            
            //////--- 3rd way copy file from source to destination is working tille android 9
            //file.CopyTo(newpath);



I have try 3 ways to copy file from source to another all ways are working till android 9 but not working after android 9.

--Android Manifest file


<?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" 
 android:versionName="1.0" package="com.NewWave.FruitApp" android:installLocation="auto">
 <uses-sdk android:minSdkVersion="23" android:targetSdkVersion="30" />
 <application android:requestLegacyExternalStorage="true" android:label="FruitApp" 
 android:theme="@style/MainTheme"></application>
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 <uses-permission android:name="android.permission.CALL_PHONE" />
 <uses-permission android:name="android.permission.SEND_SMS" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" 
 android:maxSdkVersion="29" />
  <uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" />
  <!--<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />-->
  </manifest>
Posted
Comments
Richard Deeming 29-Jun-21 5:05am    
/storage/emulated/0/Documents/FruitsApp/Backup/Fruits.db_2021-06-28 12:20:20

I don't know about Android, but that's definitely not a valid file path on a Windows device - the colon character (:) is not allowed in a file or directory name.

And since it looks like you're running your code in an emulator, it's possible that the emulator is trying to map that path directly to a path on your computer, and failing.
Prashant Sharma 29-Jun-21 9:11am    
sir same path is working with android 9 but not working with android 10 or above

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900