Xamarin.Forms - Save files out of app Directory - xamarin

I'm using Xamarin.forms Media.Plugin and it works well. But I need to save the image out of app directory, like de Download Path or DCIM Path. How to do this ?

you could look at the documentation for External Storage
for Private External Storage:
var path = GetExternalFilesDir(Android.OS.Environment.DirectoryDownloads);
string fileName = System.IO.Path.Combine(path.ToString(), "reservation.txt");
it will give a path like:
/storage/emulated/0/Android/data/package name/files/Download/reservation.txt
/storage/emulated/0/Android/data/package name/files/Download/reservation.txt
for Public External Storage:
var path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads);
string fileName = System.IO.Path.Combine(path.ToString(), "reservation.txt");
it will give the path like:
/storage/emulated/0/Download/reservation.txt
so if you want to save files out of app Directory(Private External Storage),you could save them to Public External Storage

Related

Which Environment.SpecialFolder.XXX will give me Context.getFilesDir()?

I want to access a folder in which I can save temporary files and write there a file and where I don't need permission to write to the folder.
I currently use:
string targetBaseDir = Environment.GetFolderPath(
Environment.SpecialFolder.Personal,
Environment.SpecialFolderOption.Create
);
This gives me a private directory for which I need no permissions which is the directory that corresponds to Context.getFilesDir(), however as I don't need the file to exist permanently it would be cleaner if I could save them in the directory that corresponds to Context.getFilesDir(). What do I have to write instead of .Personal to get the directory?
You can use FileSystem.AppDataDirectory via Xamarin.Essentials in your .NetStd library to obtain what is the "getFilesDir()" location on Android.
Example:
var appData = Xamarin.Essentials.FileSystem.AppDataDirectory;
appData equals /data/user/0/com.sushihangover.FormsTestSuite/files
And the Android FilesDir:
Log.Debug("SO", FilesDir.ToString() );
Equals /data/user/0/com.sushihangover.FormsTestSuite/files
access a folder in which I can save temporary files
In that case use the Cache dir:
var cacheDir = Xamarin.Essentials.FileSystem.CacheDirectory;
cacheDir would equal: `/data/user/0/com.sushihangover.FormsTestSuite/cache
re: https://learn.microsoft.com/en-us/xamarin/essentials/
Both Environment.SpecialFolder.Personal and Environment.SpecialFolder.MyDocuments will give you the path of Context.getFilesDir().
However, if you want to save small temporary files, you can make use of the cache directory Context.getCacheDir().
There is no special folder enum for the cache directory though. You'll have to get the path from the current context Context.CacheDir.Path, or from Xamarin.Essentials.FileSystem.CacheDirectory if you use Xamarin.Essentials API.
Read more:
File access with Xamarin.Android with SpecialFolder
Writing cache file in internal storage
File system helpers in Xamarin.Essentials

Retrieve StorageFile with the help of path from Local Folder

I am making Windows 8.1 Universal App and want to retrieve file,
(local -> folder -> sub Folder -> file) which is stored in Local Folder.
One way is open Local Folder, then open folder, then open sub folder, then file.
But If I know the path, can I simply open the file?
(Like in Windows Phone 8, store.OpenFile())
Is there an API which would return Storage File when passed path?
Example: GetStorageFile("local/folder/subFolder/file") ?
You can use StorageFile.GetFileFromApplicationUriAsync and pass in a URI such as ms-appdata:///local/folder/subfolder/file.txt. This is preferred over GetFileFromPathAsync (#Parad1s3's suggestion) since it is relative to your data folder and won't change if the location of the app changes (eg, on a phone the user can move to or from the SD card).
string path = ApplicationData.Current.LocalFolder + "\\" + "YourFolder" + "\\" + "YourSubfolder" + "\\yourfile.xml";
StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile file = await folder.GetFileAsync(path);
Hope this helps.

Laravel uploading a file to the right directory

I upload the files alright. Only it turns out that, in my case, the starting directory is Public, and that results that the files are accesible by everybody when they should be in the directories that are protected by previous authentication, that is, under App.
So, when I do this:
$file = Input::file('fichero');
$destinationPath = 'uploads/folder';
$file->move($destinationPath);
// Create model
$model = new Model;
$model->filename = $file->getClientOriginalName();
$model->destinationPath = $destinationPath;
The file is saved in here: localh.../myweb/public/
and that is outside the App parent folder where all my application files are.
I can imagine that the fact that is going to that directory is because it is taking the default in the configuration file, which is localhost/laravel/public/index.php/
so, writing something like
$file->move(base_path().'/uploadfolder/subdirectory/', $newname);
will start from what is fixed as base_path, but the base_path needs to be something like localhost/mywebname/public.
So, how do you write the path so that it goes to directories under App and not under Public?
I hope I am not asking a dumb question, but I have been trying for days and I have read the 3 related queries in here.
thank you
You may use:
app_path() // Path to the 'app' folder
app_path('controllers/admin') // Path to the 'app/controllers/admin' folder
This will return you local file system path, you should put your uploaded images in public/... folder.

Not sure how Directory.CreateDirectory() works

I have this code in my controller:
public ActionResult Upload(ScormUploadViewModel model)
{
if (ModelState.IsValid)
{
if (model.ScormPackageFile != null)
{
string zipCurFile = model.ScormPackageFile.FileName;
string destinationDirectoryName = Path.GetFullPath(zipCurFile);
//.GetFileNameWithoutExtension(zipCurFile);
Directory.CreateDirectory(destinationDirectoryName);
}
}
}
I upload a zip file through my view and then need to unzip it in the same location in a folder with the same name as the zipfilename
the file is: C:\TFSPreview\Zinc\Web\Project\ScormPackages\Windows 8 Training SkyDrive - Spanish.zip
I need to create a folder in C:\TFSPreview\Zinc\Web\Project\ScormPackages\
with the name: Windows 8 Training SkyDrive - Spanish
thus have: C:\TFSPreview\Zinc\Web\Project\ScormPackages\Windows 8 Training SkyDrive - Spanish\
and UNZIP in this above folder all the files contained in C:\TFSPreview\Zinc\Web\Project\ScormPackages\Windows 8 Training SkyDrive - Spanish.zip
so my question is: will CreateDirectory() create the folder Windows 8 Training SkyDrive - Spanish in C:\TFSPreview\Zinc\Web\Project\ScormPackages\ or will it try and create the folder in just c:??
thanks
It will create the directory inside C:\TFSPreview\Zinc\Web\Project\ScormPackages\. In fact, it will create all directories in that path if they don't already exist:
Any and all directories specified in path are created, unless they
already exist or unless some part of path is invalid. The path
parameter specifies a directory path, not a file path. If the
directory already exists, this method does not create a new directory,
but it returns a DirectoryInfo object for the existing directory.
However, this code has a bug: destinationDirectoryName is not the path to a directory, it's a path to a file inside the destination directory. So what you should be doing is
// zipCurFile = C:\...\ScormPackages\Windows 8 Training SkyDrive - Spanish.zip
// Path.GetDirectoryName gives "C:\...\ScormPackages"
// Path.GetFileName gives "Windows 8 Training SkyDrive - Spanish"
// Path.Combine on these two gives you the correct target
Directory.CreateDirectory(
Path.Combine(
Path.GetDirectoryName(zipCurFile), Path.GetFileName(zipCurFile));

Receiving the path of a VS2010 extension?

I've written a small Extension for VS2010 (vsix file), within the vsix is a binary file, which gets called by the extension.
I try to open the file like this:
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = #".\Binaries\TFPT.exe"}
Which works fine if I debug everything. But if I install it, I get a "file not found" error. The Binaries are installed correctly.
So I thought I try to get the complete path to the binaries. But how can I get the path? All Application.Path infos are not pointing to the correct path. Is there a way to get the path of the extension directory?
The path is something like:
C:\Users[username.domain]\AppData\Local\Microsoft\VisualStudio\10.0\Extensions[Company][ExtensionName]\1.0
Any idea without putting it together by hand?
How about retrieving the path from the current executing assembly and use Path.Combine() with your the remaining relative path segment ?
Note: This piece of code comes from a SO answer.
private static string RetrieveAssemblyDirectory()
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
var uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
You can get this path asking for your extension installation path to Visual Studio:
var serviceProvider = [YourPackageObject] as IServiceProvider;
var manager = serviceProvider.GetService(typeof(SVsExtensionManager)) as IVsExtensionManager;
foreach (IInstalledExtension extension in manager.GetInstalledExtensions())
if(extension.Header.Name == [MyExtensionName])
return extension.InstallPath;

Resources