I'm developing a TrainingPlan React-Native App.
I want to store the data in Realm database.
When I create the default database file, it works great:
let realm=new Realm({path:'training.realm});
realm.write(() => {
realm.create('Training', { type: 'group', members: 12, });
});
It creates the training.realm in this path and works well (I/O): /data/data/com.realmtmp/files/training.realm
If I want to change tha path, where to create the training.realm file, I'm using this:
let realm=new Realm({path:'/storage/emulated/0/training.realm'});
realm.write(() => {
realm.create('Training', { type: 'group', members: 12, });
});
This creates the file, but shows "Error Read-only file system".
In this path I can create folders, copy files to there.
Why does this show read-only?
(Same error on Android Studio AVD and real USB connected mobile phone.)
Regards,
Imre
Related
I'm totally new to Cordova and my current project happens to be implemented with that. I would need a way to read and list files inside a directory located at Internal storage/bap.
I've tried almost every possible way Google has given to me but without luck. If someone has any idea or could point me in the right direction, it would be really appreciated!
Edit
Managed to at least log the files with the following function, just had to be exact with the path and pass a full native path to the desired directory:
function listDir(path: string): any {
window.resolveLocalFileSystemURL(
path,
function(directory) {
var reader = directory.createReader();
reader.readEntries(
function(entries: any) {
console.log("entries inside listDir");
console.log(entries);
},
function(err: any) {
console.log(err);
}
);
},
function(err) {
console.log(err);
}
);
}
My question is that, if it is possible to get a variable from a .properties or .txt file. I need that in order to save the endPoint, the server IP address. That way, every time that the IP address is changed, all you need to do is change the IP on the file, instead of compile the code again and build the apk, and install it again on the device. Any tips will be welcome.
UPDATE
I've been trying to use this plugin :File , but with no sucess. I want to use readAsText(path,file) but all i get is undefined
import { File } from '#ionic-native/file/ngx';
constructor(
....
private file : File,
....
) { }
async test(){
this.promise = this.file.readAsText('file:///data/','ipAddress.txt');
await this.promise.then(value => {
console.log(value)
}).catch(error=> console.log("nao existe"))
}
And i'm getting the following error:
ERROR Error: Uncaught (in promise): FileError: {"code":1,"message":"NOT_FOUND_ERR"}
UPDATE
Problem Solved. I was trying to acess to the internal storage, and the document was on the external storage. Make sure you use URI, path on android are different from computer. You need URI.
file:///storage/emulated/0/Android/data/
I called the method right on, when device's ready on app.component.ts
ngOnInit() {
.....
this.initializeApp();
}
initializeApp() {
this.platform.ready().then(() => {
this.api.getURL();
.....
});
}
Hope i can help the next with the same problem I had.
We've got a managed expo app, which is using the FileSystem.downloadAsync and this is all okay.
We've got a FlatList which is displaying a list of downloaded files.
We don't want to build in a file viewer, for every app under the sun, that's not the aim.
We want to be able to click a file from the list and potentially using the Android Intent system to offer the user a list of apps they already have installed which can handle the file type.
e.g. We've got something like
class FileBrowser extends Component {
state = {
files: [
{
key: '1'
name: 'file.jpg',
sys_path: 'file://blah.jpg'
}
]
}
openFile(item) {
IntentLauncherAndroid.startActivityAsync(
'android.intent.action.OPEN_DOCUMENT', {
data: item.sys_path
})
}
render() {
return (
<FlatList data={ this.state.files } renderItem={ (item) => <Button title={ item.name } onPress={ this.openFile(item) } />} />
)
}
}
We've pretty much exhausted the list of intents from https://chromium.googlesource.com/android_tools/+/febed84a3a3cb7c2cb80d580d79c31e22e9643a5/sdk/platforms/android-23/data/activity_actions.txt
The only one that almost gets there is android.intent.action.VIEW however that just opens a random list of apps, none of which can handle images - We've also passed in the mime type e.g. image/jpeg or image/* hoping that will filter the list.
What would be the correct way on Android only to offer the user to open the file in an appropriate apps?
with Expo SDK 34 (I use expo 34.0.3 because of there is font not resolve issues), you can do the open list as this way. but this is not a proper solution to open list. because the user has to do some work like this.
expo sharing api
await Sharing.shareAsync(
fileUri,
{dialogTitle: 'share or copy your pdf via'}
).catch(error =>{
console.log(error);
})
if you do this with providing a local file URI, you can see this bottom action sheet as below. then the user can choose the copy to option and paste it some location. after that user can open the file and then OS ask for what app will be used to open your file.
I am trying to exchange files between a .Net Desktop App and a Xamarin.Forms Android app. I really had trouble getting resources and help, so I want this question to also wrap some stuff up - for other interested users (not only of Xamarin Forms)
So far I got to read files and folders in .Net C# using
Christophe Geers' Blog and Fun with MTP and because writing did not work on all of my devices Windows File Stuff by Flauschig
On Xamarin.Forms side of the wall I first stumbled over Xamarin doc and A lib to store from pcl code and also Plugin.Permissions.
With all these sources above I was able to get reading and writing on the Android (Samsung A5, Android 6.0) to work.
With this description on filestructure I thought the following Code will always get a valid path:
public string GetExternalFolder()
{
GetPermission();
var externalFolder = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, "Android", "data", "com.exampledom.appname", "files");
if (!Directory.Exists(externalFolder))
CreateFolderStructure();
return externalFolder;
}
GetPermission() is taken from Plugin.Permissions and will throw exception if permission is not given.
CreateFolderStructure() is using pcl.storage - and looks like this.
private async void CreateFolderStructure()
{
try
{
IFolder rootFolder = await FileSystem.Current.GetFolderFromPathAsync(Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path));
var subpaths = new[] {"Android", "data", "com.exampledom.appname", "files"};
foreach (var dir in subpaths)
{
if (!string.IsNullOrEmpty(dir))
rootFolder = await rootFolder.CreateFolderAsync(dir, CreationCollisionOption.OpenIfExists);
}
Log("Folder structure created!");
}
catch (Exception e)
{
Log("Folder structure cannot be created!");
Console.WriteLine("Failed to log! " + e.Message);
}
}
Ok, now for the question:
The Folder that is created by this Method is only created on one device I tested on (proof of concept) - all others (eg Samsung A3, Android 6.0) nothing happens(proof of helpless developer). A reboot is not the solution.
Also on the device this works on - the permission is requested as expected. There is no permission request on the other devices.
Why is the desired folder not created during install? It seems all other apps do have such a folder 'Android/data/com.exampledom.appname/files'.
What am I missing? It seems overly hard to import/export to android.
#smack Does CreationCollisionOptions.OpenIfExists really create a folder? I don't think so. Is there not CreateIfNotExists enum?
I'm using Ionic Framework to build an iOS / Android app which writes sqlite data to a CSV file, then attaches that file to an Email and sends it. The following code works correctly on iOS (actual device iPhone 5).
I don't have an Android device, but in the Android emulator (nexus 5), the file sent never has a file attachment (despite the emulator showing that it does).
Is there a different way I should be writing this code?
I looked at the documentation here, but it does not clarify
https://github.com/katzer/cordova-plugin-email-composer#adding-attachments
$cordovaFile.writeFile(cordova.file.dataDirectory,
"PatientEncounters.csv",
data.join("\n"),
true)
.then(function (success) {
$cordovaEmailComposer.isAvailable().then(function() {
var emailOpts = {
to: [email],
attachments: ['' +
cordova.file.dataDirectory.replace('file://','') + "PatientEncounters.csv"],
subject: 'Patient Encounters',
body: 'A CSV containing Patient Encounters is attached',
isHtml: false
};
$cordovaEmailComposer.open(emailOpts).then(null, function () {
// user cancelled email
});
return;
}, function (error) {
return;
});
}, function () {
// not available
});
My problem was using cordova.file.dataDirectory instead of cordova.file.externalDataDirectory. The mail app in android would not allow attaching files from internal storage.