I am trying to write a file in the internal storage directory of my application with the following step :
1) Initializing my "jni library" in Acivity Class :
MyLib mylib = new MyLib();
2) Give the internal storage path by calling getFilesDir in my Activity Class:
mylib.setSavePath(getFilesDir());
3) Call a method mylib.save() from my library which is doing the following in c++:
Open the file which i want to write with :
fp = fopen(pathtotheinternalstorage+filename,"w");
if (!fp) {
SetError(XML_ERROR_FILE_NOT_FOUND, filename, 0);
return _errorID;
}
The file path is correct : /data/data/com.myapp/files/myfile.xml
But fopen fails, i dont know what i am doing wrong.
If i write with some java code (openFileOutput), it is working well.
Thanks for your help.
More information would be helpful. Based on the path, I'm assuming you're on a Linux box. I would first check the permissions of the path. A few suggestions below
1) Try opening a file in your home directory and see if you have the same issue.
2) Try setting the file world writable using chmod 777 and see if that fixes the problem. Remember setting world writable is really INSECURE and you should change it to something more restrictive once you've found the problem.
I found the answer, i was using a library that was redefinig fopen to read inside asset folder.
#define fopen(name, mode) android_fopen(name, mode)
now it s working
Related
I am making an app which use the following java line:
Net dnnNet = Dnn.readNetFromONNX("path\to\file");
As you can see, readNetFromONNX requires path to onnx file.
So I put my onnx file under assest folder, so I can use it at run time.
The problem is I you can only read assets foler with AssestManager and inputstream... the readNetFromONNX needs path...
How do I overcome this probelm? Is there a way to get the path of the file at run time? Maybe other folder?
Thanks from advance
I've recently tried porting an engine I've been working on to Android and have ran into some problems when trying to use "require" or "dofile" within a lua script.
(quick note: this is written in C++11, using ndk-build and ant to compile on windows 7)
Compiling Lua (Version 5.3) was pretty simple and I used the following article to get access to the internal assets directory:
50ply blog post on loading compressed android assets
I added an output in the replaced fopen function to help debug this issue and when I run:
luaL_dofile(LuaS, "scripts/test.lua");
I get:
>> scripts/test.lua , read
Which is perfect for me and runs the file in the assets/scripts folder, but when I try to run the following line in the lua script:
local derp = require("scripts.noop")
I get:
>> /usr/local/lib/lua/5.3/scripts/noop.so , read
After looking through the Lua source code, this path seems to be the "LUA_CDIR" as defined in "luaconf.h", which also explains why it looks for *.so files instead of *.lua... So I'm not sure why it's looking for the LUA_CPATH instead of the LUA_PATH or how to fix this.
If anyone could point me in the right direction, that would be great and if I could do this by overwriting the search path/settings outside of the Lua source, it would be even better.
Sorry if this question is not well written, if any further information is required, I will provide it. I'm in a bit of a rush right now.
Thanks for reading.
Require checks both LUA_PATH and LUA_CPATH with many different combinations found within package.path and package.cpath. Environment variables LUA_PATH and LUA_CPATH can be found merged into package actually, as preparation goes.
Psuedo-c/c++-example:
lua_getglobal(L, "package");
lua_getfield(L, -1, "path");
lua_getfield(L, -2, "cpath");
const char* cpath = lua_tostring(L, -1);
const char* path = lua_tostring(L, -2);
lua_pop(L, 3); // field 2, field 1, package table
printf("cpath: `%s`\n", cpath);
printf("path: `%s`\n", path);
Which is almost equivalent to, minus the print calls:
>print(package.cpath)
.\?.dll;C:\Program Files\LuaConsole\bin\?.dll;C:\Program Files\LuaConsole\bin\loadall.dll
>print(package.path)
.\?.lua;C:\Program Files\LuaConsole\bin\lua\?.lua;C:\Program Files\LuaConsole\bin\lua\?\init.lua;
Here is an example of require working:
>require("abc")
(Runtime) | Stack Top: 0 | [string "require("abc")"]:1: module 'abc' not found:
no field package.preload['abc']
no file '.\abc.lua'
no file 'C:\Program Files\LuaConsole\bin\lua\abc.lua'
no file 'C:\Program Files\LuaConsole\bin\lua\abc\init.lua'
no file '.\abc.dll'
no file 'C:\Program Files\LuaConsole\bin\abc.dll'
no file 'C:\Program Files\LuaConsole\bin\loadall.dll'
--
stack traceback:
[C]: in function 'require'
[string "require("abc")"]:1: in main chunk
[C]: at 0x00401c80
The solution of your problem is to have your assets directory fall into a directory found within package.cpath or package.path. As #GabeSechan suggested in the comments, "You'll probably have an easier time of things if you copy the files from assets to the actual filesystem. Assets is not a directory on the device, so anything looking for a file won't find it." So if you are able to get this situated, go for it!
I can't run my python program on my android device using qpython3.
The first step in he program is to create a text file to save data to it. But I get an I/O error (file system read only)
This is the function used to create / or be sure that the file exists easily.
def filecreate(file): # creates the text file
f = open(file, 'a')
print('file created successfully\n')
print()
f.close()
How to overcome this problem in android ?
QPython by default runs your programs from the root directory which is not writable by non-root users. If you check the ftp utility (under About menu) you will find the name of the directory that is writable by QPython. On my HTC it is:
/storage/emulated/0/com.hipipal.qpyplus
So you need to do something along the lines of:
RootPath='/storage/emulated/0/com.hipipal.qpyplus'
...
import os
f = open(file, os.path.join(RootPath,'a'))
...
Example of a similar problem with QPython and SQLite3
Just create the file with QPython before use in your script.
You can create an txt file with QPython.
In My case I create the file whith name "pontoText.txt"
Inside folder "MyPythonScripts" (Im create that folder to put my python files)
so the file is in the same folder of the script and are created in QPython
Now an example code im make ton test:
import time
import os
a = input("1 to save date on file or 2 too see records on file. ")
folder= '/storage/emulated/0/MyPythonScripts'
if(a == "1"):
pontoTxt=open(os.path.join(folder,'pontoText.txt'),'w')
pontoTxt.write(time.strftime("%I %M %p on %A, %B %e, %Y"))
if(a == "2"):
pontoTxt=open(os.path.join(folder,'pontoText.txt'),'r')
exibe = pontoTxt.read()
print(exibe)
Your app may not have required permission to manage that file. Check your manifest, specify Android version and file path. Check documentation: http://developer.android.com/training/basics/data-storage/files.html
You have to actually find the file path, as it doesn't infer the directory that the file is in as the destination directory, unlike in Python on the PC.
Use what they're changed.
The file you have is inside /accounts/mastercard root and the file you're inside is /accounts/main.py you only need to use is this
/mastercard/xxxxx like my code
from django.shortcuts import render
from django.http import HttpResponse
from . import models
# Create your views here.
def index(request):
return render(request,"mastercard/Templates/mastercard/Base.html")
def about(request):
return render(request,"mastercard/Templates/mastercard/index.html")
I'm trying to create a partition called "subdata" under "/data". But it's failing.
The steps I tried and the failure results are mentioned below.
File dir =new File(/data/subdata/");
boolean success = dir.mkdir();
Here, the "success" value is found "false".
File dir= context.getDir("/data/subdata",Context.MODE_WORLD_READABLE);
Here, I get "java.lang.IllegalArgumentException: File app_/data/subdata/ contains a path separator"
Please, help me in creating this subfolder under /data/ partition.
I solved my need in the below manner.
As I mentioned in the question that, if I create the sub folder manually, I'm able to read and write to that subfolder in the /data.
So, I created this subfolder through init.rc (as I mentioned, I have the build code also). As I'm more fluent in Linux than in Android, I fixed it through init.rc. Now I'm able to read/write to that folder through my android code.
I have installed QPython in my Android mobile.
I written a statement in the QEdit to read a text file from the below path
/storage/emulated/0/com.hipipal.qpyplus/script3/File1.txt
I used the below statement
fob=open('/storage/emulated/0/com.hipipal.qpyplus/script3/File1.txt','r')
fob.read()
If I run the statement, it is throwing error as:
IOError:[Errno 2] No such file or directory: '/storage/emulated/0/com.hipipal.qpyplus/script3/File1.txt'
1|uo_a116#cancro:/ $
Is the above statement correct?
fob=open('File1.txt','r')
Is not working in version 1.0.4.
fout=open('File2.txt','w')
Was working on version 0.9.6, but is not working in 1.0.4.
The "error" is Read only file system.
It looks like restrictions in the (new 1.0.4) file system library. I post a mail to the editor, but no answer at this time.
For testing, try to write absolute path to your files pointing, for example, to sdcard (/sdcard/out.txt).
I had problems on this versions (>=1.0.4) because launch process of script changes and execution directory is not the same as script directory.
I had to change my scripts to point to absolute paths.
It was tested with qpython developer.
Check this link:
https://github.com/qpython-android/qpython.org/issues/48
You can also try as simple as:
fob=open('File1.txt','r')
fob.read()
Just if the script is in the same folder of the file.
You can change the current working directory to path with script before read file:
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))