Working with files

RamtinJokarRamtinJokar USMember ✭✭
edited August 2017 in Xamarin.Forms

Hi, I'm trying to create an uploader app.
I created this app in UWP before, now I want to create this in xamarin.forms.

I used file picker (from Xam.Plugin.FilePicker nuget) as my file picker.
after picking file from removable storage(external storage - sd card) bytes array length is 0 but picker can get file name.

permissions:

    <uses-permission android:name="android.permission.INTERNET" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

here is my code

                // Check and grant storage access from user
                await BaseR.CheckAndGrantAccess();

                // check network available
                if (!BaseR.GetInternetConnectivity())
                {
                    await DisplayAlert("No network", "No internet connection found.\r\n" +
                        "Please connect to internet and try again", "OK");

                    return;
                }
                FileData filedata = new FileData();
                filedata = await CrossFilePicker.Current.PickFile();
                if (filedata == null)
                    return;

                Debug.WriteLine("fileData " + (filedata.DataArray == null ? " Is " : " Not ") + " null");
                Debug.WriteLine("Bytes: " + filedata.DataArray?.Length);
                Debug.WriteLine("FileName: " + filedata.FileName);

Debug output:

[0:] fileData  Not  null
[0:] Bytes: 0
[0:] FileName: CTR.PNG

as you see bytes array is 0, I tested this app in real phone with Android 7.0.

What is the problem?
Is there anything I have to do?

Thanks

Ramtin

Best Answer

  • RamtinJokarRamtinJokar US ✭✭
    Accepted Answer

    Hi, after 8 hours I just re-install this nuget package a couple times and now it's works well :|
    I don't know what was the issue, maybe installed package was damaged.
    I marked this as A answer.

    Ramtin

Answers

  • NPCNPC USMember ✭✭
    edited August 2017

    I am confused by your post, I do not know what a FileData is and I do not know how you are using the Picker. I can offer some advice for getting files off of the External Storage on Android.

    Generally when working with the filesystems in a PCL project, you will have to write an Interface in the PCL portion. And implement the Interface in the platform specific version.

    For Android something like this could be used:

    var dir = Android.OS.Environment.ExternalStorageDirectory;
    /*
    Add to the Path to get wherever you need to
    */
    var stream = File.Open(dir, FileMode.Open);
    byte[] arr = new byte[stream.Length];
    stream.Read(arr,0,(int)stream.Length);
    
  • RamtinJokarRamtinJokar USMember ✭✭
    Accepted Answer

    Hi, after 8 hours I just re-install this nuget package a couple times and now it's works well :|
    I don't know what was the issue, maybe installed package was damaged.
    I marked this as A answer.

    Ramtin

Sign In or Register to comment.