I created a PCL project in Xamarin and I am trying to save files with a cross-platform solution using PCLstorage.
This is my code (from the example in PCLstorage website)
IFolder rootFolder = FileSystem.Current.LocalStorage;
IFolder folder = await rootFolder.CreateFolderAsync("FolderName", CreationCollisionOption.OpenIfExists);
IFile file = await folder.CreateFileAsync("filename.txt", CreationCollisionOption.ReplaceExisting);
await file.WriteAllTextAsync("text");
Now, I want to know where this file is saved.
In Windows Phone, using WP power tools, I can explore the isolatedStorage of my app and I find and open the txt file. While, in Android, I can't find the folder created!
The path would be "data/data/com.appname.test/files/ but I don't find it!
Someone can help me?
You will be able to access to that folder only from the device (not from PC using USB cable) and only with root rights, so you need to root your device (try SuperSu from GooglePlay it work for most of the devices)
if you want to store it in external storage you can pass the folder path and create the file. so you can access that file physically.
string path="path";
IFolder rootFolder = await FileSystem.Current.GetFolderFromPathAsync(path);
IFolder folder = await rootFolder.CreateFolderAsync("folder", CreationCollisionOption.OpenIfExists);
IFile file = await folder.CreateFileAsync("file", CreationCollisionOption.ReplaceExisting);
Related
I'm developing an Android App using flutter, which downloads some images from internet and stores them in a directory inside internal storage (the directory and it's content should be visible to user).
I tried path_provider package but it did not work. Here is my code:
var directory = await getExternalStorageDirectory();
print(directory);
Terminal Output (tested on real Android device without SD Card):
W/ContextImpl( 6856): Failed to ensure directory: /storage/extSdCard/Android/data/com.example.test.test/files
I/flutter ( 6856): Directory: '/storage/emulated/0/Android/data/com.example.test.test/files'
I want the root directory of internal storage but it returns app's specific local directory. So I decided to hardcode /storage/emulated/0/ as root directory.
Is this solution safe?
If not, how can I solve the problem?
I am developing a file based application through flutter.
I can create a folder through getApplicationDocumentsDirectory() which gives the path to write files. But it cannot be seen in the files Explorer
I then created the folder through getExternalStorageDirectory() , which can be seen in the files explorer. But I want it to be created in the root.
You may have seen whatsapp folder in the root directory. I also want the same thing. I have tried the following:
Directory('FolderName').create()
But it gives the error saying 'read only os '
Is there a way to do it through flutter ?
You can do it this way:
String folderName = "My New Downloads";
var path = "storage/emulated/0/$folderName";
await new Directory(path).create();
I am building a file locker app in flutter which can lock files as well a folders. When a user unlocks a folder I want my app to show an option to view folder contents but in native file explorer. Is there any way to implement this feature?
(Thanks in advance..)
You can try this package for the use case you've mentioned: open_file
Usage:
import 'package:open_file/open_file.dart';
import 'package:path_provider/path_provider.dart';
//Get the path to the directory
final String dir = (await getTemporaryDirectory()).path;
//Use the path to launch the directory with the native file explorer
await OpenFile.open('$dir\\');
(Note: Tested on windows OS only)
After a lot of googling, and a lot of tries with "out-of-context" code, I'm begging you to help me.
I'm trying to figure out if it's possible to write on the external storage with Nativescript. I'm not interested to write within the application context. So what the docs shows, it's not what i'm looking for.
I've managed to achieve this from a thread on the Nativescript forum:
android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOWNLOADS).toString();
It works, it gives me a path, but when I have this path I have no clue of what to do with it. How to create a file inside that path, read it etc.
What I need to achieve is to create a folder that both the user and the application can easily access. The user should be able to access this folder with the builtin files explorer.
The application runs on Angular.
I really struggled with this one on Android device and in the end it was due to:
Not making sure the required permissions has been granted by the user in the app
Using "Android File Transfer" on my Macbook to verify the files have been created and to download them
tns info
nativescript 6.0.3
tns-core-modules 6.0.7
tns-android 6.0.2
tns plugins
nativescript-permissions 1.3.7
example code
import * as fs from "tns-core-modules/file-system"
...
// First get the required permissions
// Note: this permissions should also be in your AndroidManifest.xml file as:
// <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
const permissions = require('nativescript-permissions')
permissions.requestPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
.then(() => {
console.log('Required Android permissions have been granted');
})
.catch(() => {
console.error('Required Android permissions have been denied!');
});
// Get the publicly accessable Downloads directory path
const sdDownloadPath = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOWNLOADS).toString()
console.log('sdDownloadPath: ' + sdDownloadPath)
// Get a specific folder in that path (will be created if it does not exist)
const myAppFolder = fs.Folder.fromPath(fs.path.join(sdDownloadPath, 'myApp'))
console.log('myApp path: ' + myAppFolder.path)
// Get a file in that path (will be created if it does not exist)
// Note: In this case we try to get a unique file every time this code is run
let date = new Date()
date = date.toISOString().replace('.', '')
const myFile = myAppFolder.getFile(`myfile_${date}.txt`)
console.log('myFile path: ' + myFile.path)
// Write some data to this new file
myFile.writeText('Hello duder 123')
.then(() => {})
.catch((err) => console.log(`Error writing to file: ${err}`))
// Try and read back the data that was written
myFile.readText()
.then((res) => {
console.log(`Text read back: ${res}`)
}).catch((err) => {
console.log(err.stack);
});
// List all files in the myApp folder
myAppFolder.getEntities()
.then((entities) => {
// entities is array with the document's files and folders.
entities.forEach((entity) => {
console.log(entity)
});
}).catch((err) => {
console.log(err.stack);
});
android file transfer issue
One problem I wasted a lot of time on was that I could see the files with getEntities() but could not see them when using the 3rd party tool "Android File Transfer (AFT)" on Mac. I eventually stumbled across "Android Studio's -> Device File Explorer" and could see all my created files and folders with it so realised the issue is with AFT.
I now make use of Airdroid to browse and download device files.
applicable reference
https://docs.nativescript.org/angular/ng-framework-modules/file-system
(angular docs but relevant to nativescript-vue as well)
Do you know your external(sd card) path?
If it is like /storage/emulated/0, then you could try this to create a folder or file.
import * as fs from "tns-core-modules/file-system";
let externalPath= fs.path.join(android.os.Environment.getExternalStorageDirectory().getAbsolutePath().toString());
//Create a folder with known path
var folder: fs.Folder = fs.Folder.fromPath(sdCardPath+"/test");
//Create a file
var testFile: fs.File = folder.getFile("test.txt");
console.log("Path " + folder.path)
User should be able to access this fold and file. It is in device internal storage which is "external" folder.
I still try to figure out how to get access to sd card but hope above code work for you.
I have the same issue and finally solved it by adding android:requestLegacyExternalStorage="true" inside the AndroidManifest.xml file
follow the thread here
I have a db located at
ctx.getApplicationContext().getDir("data", 0) + "/" + "db.db4o";
How can I browse this file with an file manager or via USB? I can't seem to find it :<
Browse with file manager, or USB, what does that mean? You can access the data directory, through a file manager, only if you are on a rooted device or an emulator.
Your application specific file are not directly stored in the data directory, but
data/data/your_package_name
you could create an "asset" folder into your android project and put it there. Before that you can try with this:
InputStream is = getAssets().open("db.db4o");
And convert "is" if is necessary.
Regards