PCLStorage icon indicating copy to clipboard operation
PCLStorage copied to clipboard

Adroid write to files.

Open sstahurski opened this issue 8 years ago • 4 comments

Having problems writing then reading files Xamarian Android application.

Its the same issue posted to here.... http://stackoverflow.com/questions/21999804/cross-platform-solution-to-isolated-storage-in-pcl/37439443#37439443

I have my permissions set correctly to read/write external storage in my android manifest file.

Reading the data I get this exception ex {PCLStorage.Exceptions.FileNotFoundException: File does not exist: /data/data/com.bconnect.expo/files…} PCLStorage.Exceptions.FileNotFoundException

This is after the write appears to be successful.

Shouldn't the file name be '/Android/data/com.bconnect.expo/files....' ?

Here is my code, as you can see I'm not doing really anything more than what the examples give

    private async void writeDeviceData ()
    {
        IFolder rootFolder = FileSystem.Current.LocalStorage;

        //load everything into a dictionary
        Dictionary<string,string> expoContents = new Dictionary<string, string>();

                    //omitted.... for size

        //create file
        IFile outFile = await rootFolder.CreateFileAsync("ExpoFile.txt", CreationCollisionOption.ReplaceExisting);

        //write to file
        var strContents = JsonConvert.SerializeObject(expoContents);

        try
        {
            await outFile.WriteAllTextAsync( strContents );

        }
        catch(Exception ex) 
        {

        }

    }

    private async void readDeviceData ()
    {

        IFolder rootFolder = FileSystem.Current.LocalStorage;

        try{
            IFile inFile = await rootFolder.GetFileAsync("Expofile.txt");
            string strContents = inFile.ReadAllTextAsync ().Result;
                              //...omitted for size
            }
        }
        catch ( Exception ex)
        {
            //go out to web service
            reloadSiteData ();
        }

    }

sstahurski avatar May 25 '16 14:05 sstahurski

I can confirm this issue on MemoPad 7 with Android 4.4.2.

Blackhex avatar Aug 31 '16 21:08 Blackhex

I have the same problem. On iOS it works fine. But on Android I get the same bug. On Android I first write the file correctly (I checked with the AndroidDeviceMonitor file browser). So I'm sure that the file is there and the path is correct. Then I load it from the cache, and then I get the file not found error. This is the exception:

GetCachedBadge: PCLStorage.Exceptions.FileNotFoundException: File does not exist: /storage/emulated/0/toolbar/y62d2ke8.ink/[email protected]
  at PCLStorage.FileSystemFolder+<GetFileAsync>d__6.MoveNext () [0x000bc] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3540/1cf254db/source/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/3540/1cf254db/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/3540/1cf254db/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/3540/1cf254db/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in /Users/builder/data/lanes/3540/1cf254db/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:357 
  at ToolbarBadgeSample.Controls.ToolbarItemEx+<GetCachedBadge>c__async2.MoveNext () [0x000bb] in /Users/Admin/Projects/xamarin-samples-12545da4b6e715faedbb247c8a9ffa04f6d7a7e9/ToolbarBadgeSample/ToolbarBadgeSample/ToolbarBadgeSample/Controls/ToolbarItemEx.cs:348 
Could not load image named: {0}: /storage/emulated/0/toolbar/y62d2ke8.ink/[email protected]

I checked my code, and it is fine. It asks for the correct path and filename. But the PCLStorage library is not able to found it for some reasons.

Emasoft avatar Sep 10 '16 02:09 Emasoft

@Emasoft I'm having a similar issue on uwp. I'm thinking it may have to do with file permissions.

zippo227 avatar May 09 '17 20:05 zippo227

@Emasoft I wrote up the bug for UWP, which I supppose is different from your Android bug. issue 58

I was having a bug similar to yours on Android the other day. I think part of it was that I was writing the files in parallell. Once I blocked the write function with a SemaphoreSlim, I was able to read the files after writing them.

private SemaphoreSlim writeLock = new SemaphoreSlim(1, 1);
private async Task<string> CacheFileHelper(string filename, MemoryStream stream)
{
	await writeLock.WaitAsync();

	try
	{
		IFile file;
		file = await FileSystem.Current.LocalStorage.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
		using (var fileStream = await file.OpenAsync(PCLStorage.FileAccess.ReadAndWrite))
		{
			byte[] imgBytes = stream.ToArray();
			await fileStream.WriteAsync(imgBytes, 0, imgBytes.Length);
		}

		string path = file.Path;
		return path;
	}
	catch(Exception ex)
	{
		Console.WriteLine(ex.Message);
	}
	finally
	{
		writeLock.Release();
	}

	return string.Empty;
}

zippo227 avatar May 09 '17 20:05 zippo227