I've found a workaround based on solutions on this thread
http://mono-for-android.1047100.n5.nabble.com/detect-SD-Card-path-td5710218.html


My idea is to use a linux command like in the thread linked above to
retrive the available free space. To do this I'm currently using DF. In
case of failure it use StatFs.

my 2cents:

try
{
Java.Lang.Process proc =
Java.Lang.Runtime.GetRuntime().Exec(String.Format("df {0}",
uri.AbsolutePath));
 proc.WaitFor();
 var resi = proc.InputStream;
var rdr = new StreamReader(resi);
string str = rdr.ReadToEnd();
 string[] lines = str.Split('\n');
if(lines.Length < 2)
throw new InvalidOperationException("Unable to get size from shell.");
 string[] entries = lines[1]
.Split(' ')
.Where(e => !String.IsNullOrWhiteSpace(e))
.ToArray();
 string entry = entries[3];
 ulong value = (ulong)Int32.Parse(entry.Substring(0, entry.Length - 1));
string unit = entry.Substring(entry.Length - 1, 1);
 switch(unit)
{
// Value is in bytes
case "B":
return value;
 // Value is in Kbytes
case "K":
return value * 1024;
 // Value is in Mbytes
case "M":
return value * 1024 * 1024;
 // Value is in Gbytes
case "G":
return value * 1024 * 1024 * 1024;
 default:
throw new InvalidOperationException("Unknown size unit.");
}
 }
catch (Exception ex)
{
StatFs stats = new StatFs(uri.AbsolutePath);
return (ulong)(stats.AvailableBlocks * stats.BlockSize);
                        }

On Mon, Sep 17, 2012 at 12:57 PM, Miljenko Cvjetko <
[email protected]> wrote:

>  Hi
>
> Samsung!
> Read Jon's explanation here:
>
> http://mono-for-android.1047100.n5.nabble.com/detect-SD-Card-path-td5710218.html
>
> there are few workarounds. See if those help
>
> cheers
>
> mel
>
> On 2012.09.16 21:30, Francesco Colombo wrote:
>
> Another Update:
>
> Scenario: Samsung GS2 without SD card
> External storage path: /mnt/sdcard
>
> System tells me I've got 6.34gb of free space. API tells me that I have
> 175922186204271mb of free space (they should be  6492.16mb)
>
>
> On Sun, Sep 16, 2012 at 9:25 PM, Francesco Colombo <
> [email protected]> wrote:
>
>> Another update:
>>
>> Android SDK say:
>> Every Android-compatible device supports a shared "external storage" that
>> you can use to save files. This can be a removable storage media (such as
>> an SD card) or an internal (non-removable) storage.
>>
>> So basically calling *Environment.ExternalStorageDirectory* should
>> return a valid path for either for *removable* or *internal* storage
>> right?
>> I'am quite sure about this because my Samsung G2 doesn't have a SD card
>> and I'm getting reasonable values. :(
>>
>> So my first question: "Is StatFS working on JellyBean?" remains valid!
>>
>>
>>
>>
>>
>> On Sun, Sep 16, 2012 at 9:16 PM, Francesco Colombo <
>> [email protected]> wrote:
>>
>>> Ok, I think I've discovered what's the problem is. My bad knowledge of
>>> android :(
>>>
>>> Tests have been done on a device which has not a real SD card. I'm a bit
>>> confused on how android defines spaces where apps can write into it (local
>>> and external).
>>>
>>> I need to investigate more. Any help is appreciate on explaining which
>>> options are available for an android app on "where I can write files"
>>>
>>>
>>>  On Sun, Sep 16, 2012 at 8:54 PM, Francesco Colombo <
>>> [email protected]> wrote:
>>>
>>>> Further investigations denotes that 
>>>> using*Enviornment.ExternalStorageDirectory.Path
>>>> *on jellybean return as folder */storage *in place of */mnt/sdcard. *Is
>>>> it right?
>>>>
>>>>
>>>> On Sat, Sep 15, 2012 at 6:23 PM, Francesco Colombo <
>>>> [email protected]> wrote:
>>>>
>>>>> Hi,
>>>>>
>>>>> I'm developing an app which on the first run it will download a big
>>>>> file from FTP. Before the download begin, the user is asked where to save
>>>>> the file. One can use either local or external storage.
>>>>> When the user select "external storage" the app will use as root the *
>>>>> Application.Context.ExternalCacheDir.Path* variable. That is! In this
>>>>> way all content written into this folder *will be deleted* once the
>>>>> app is removed from the device.
>>>>>
>>>>> Here my code to get available storage size:
>>>>>
>>>>> *string *external_path = Path.Combine(*
>>>>> Application.Context.ExternalCacheDir.Path*, "/mypath");
>>>>> *
>>>>> *
>>>>> *ulong *totalFreeSpace = 0;
>>>>> *StatFs *stats = *new* Stats(external_path);
>>>>> totalFreeSpace = (*ulong*)stats.AvailableBlocks * stats.BlocksSize;
>>>>>
>>>>> All this stuff is good on Android +2.2. I'm facing a
>>>>> strange behavior ONLY on JellyBean. It seems that StatFs will return the
>>>>> same available blocks value for either internal 
>>>>> (use*Application.Context.FilesDir.Path
>>>>> *in place of *Application.Context.ExternalCacheDir* )or external path.
>>>>>
>>>>> Anyone can confirm this? Am I missing something?
>>>>>
>>>>> thanks
>>>>> Francesco
>>>>>
>>>>
>>>>
>>>
>>
>
>
> _______________________________________________
> Monodroid mailing [email protected]
>
> UNSUBSCRIBE INFORMATION:http://lists.ximian.com/mailman/listinfo/monodroid
>
>
>
> --
> Miljenko Cvjetko dipl.ing. ET
>       Direktor/CEO
>       Projektant rješenja/Solution Architect  
>       Razvojni programer/Senior developer
>       Voditelj projekta/Project Manager
>
> IX južna obala 13
> Kajzerica Zagreb
> T: 385 1 7775555
> M: 385 91 557 447 3
> F: 385 1 7779556
> e: [email protected]
> w: http://www.holisticware.net
>
>
_______________________________________________
Monodroid mailing list
[email protected]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

Reply via email to