chaquo / chaquopy

Chaquopy: the Python SDK for Android
https://chaquo.com/chaquopy/
MIT License
809 stars 131 forks source link

Creating a file #234

Closed bv-noake closed 4 years ago

bv-noake commented 4 years ago

Hi, I have this code that i am using to write to a file, I know it works as i have tested it normally using python however, when i try to use it in android studio it tells me the file is read only.

images.execute("SELECT Picture FROM Pictures") record = images.fetchall() for row in record: photo = row[0]

    if os.path.exists("myfile.jpg"):
        print("The file exists")
        os.remove("myfile.jpg")
        with open("myfile.jpg", "wb") as fh:
            fh.write(photo)
            fh.close()
        image = face_recognition.load_image_file("myfile.jpg")
        known_face_encodings.append(face_recognition.face_encodings(image)[0])
    else:
        print("The file does not exist")
        with open("myfile.jpg", "wb") as fh:
            fh.write(photo)
            fh.close()
        image = face_recognition.load_image_file("myfile.jpg")
        known_face_encodings.append(face_recognition.face_encodings(image)[0])
bv-noake commented 4 years ago

D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.wit, PID: 25369 java.lang.IllegalStateException: Could not execute method for android:onClick at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:402) at android.view.View.performClick(View.java:7125) at android.view.View.performClickInternal(View.java:7102) at android.view.View.access$3500(View.java:801) at android.view.View$PerformClick.run(View.java:27336) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397) at android.view.View.performClick(View.java:7125)  at android.view.View.performClickInternal(View.java:7102)  at android.view.View.access$3500(View.java:801)  at android.view.View$PerformClick.run(View.java:27336)  at android.os.Handler.handleCallback(Handler.java:883)  at android.os.Handler.dispatchMessage(Handler.java:100)  at android.os.Looper.loop(Looper.java:214)  at android.app.ActivityThread.main(ActivityThread.java:7356)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)  Caused by: com.chaquo.python.PyException: OSError: [Errno 30] Read-only file system: 'myfile.jpg' at .facerecognition.main(facerecognition.py:64) at .chaquopy_java.call(chaquopy_java.pyx:283) at .chaquopy_java.Java_com_chaquo_python_PyObject_callAttrThrows(chaquopy_java.pyx:255) at com.chaquo.python.PyObject.callAttrThrows(Native Method) at com.chaquo.python.PyObject.callAttr(PyObject.java:207) at com.example.wit.MainActivity.takepicture(MainActivity.java:99) at java.lang.reflect.Method.invoke(Native Method)  at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)  at android.view.View.performClick(View.java:7125)  at android.view.View.performClickInternal(View.java:7102)  at android.view.View.access$3500(View.java:801)  at android.view.View$PerformClick.run(View.java:27336)  at android.os.Handler.handleCallback(Handler.java:883)  at android.os.Handler.dispatchMessage(Handler.java:100)  at android.os.Looper.loop(Looper.java:214)  at android.app.ActivityThread.main(ActivityThread.java:7356)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)  I/Process: Sending signal. PID: 25369 SIG: 9 Process 25369 terminated.

mhsmith commented 4 years ago

If you want to read a file included with your source code, please see #144.

If you want to write a file, use getFilesDir as described in https://github.com/chaquo/chaquopy/issues/177#issuecomment-557496897.

bv-noake commented 4 years ago

How would i use this line to make a jpg file:

df_signaures.to_csv(files_dir+'graduate_signatures.csv')

mhsmith commented 4 years ago

I'm not familiar with the library you're using, but if you want to construct a filename, then you'll need to do it like this:

files_dir + "/" + filename

or this:

from os.path import join
join(files_dir, filename)
mhsmith commented 4 years ago

I'm not sure why you've re-asked the original question on StackOverflow: you won't get a better answer than what I've already provided. If you need some clarification then let's continue the conversation here.

anuuzsoni commented 4 years ago

i want to write a file so that i can see it in my phone's internal storage..plz tell how to do...

mhsmith commented 4 years ago

As I wrote above, you can use getFilesDir as described in https://github.com/chaquo/chaquopy/issues/177#issuecomment-557496897.

anuuzsoni commented 4 years ago

sorry sir, but i did the same...but i cannot locate the file in my mobile's file manager...plz give a full demostration and share the related documents....thank you sir

anuuzsoni commented 4 years ago

More specifically i want to write a file to the downloads folder of my internal storage

anuuzsoni commented 4 years ago

20200823_100657 1 check this and plz help me solve

mhsmith commented 4 years ago

The Android documentation refers to the downloads folder as "external storage", although most recent phones store it on the same hardware as everything else.

Unfortunately the rules around accessing external storage are complex and have changed several times in the history of Android. For details, see https://github.com/chaquo/chaquopy/issues/38#issuecomment-388632574.