In my project, I tried to write a code using Chaquopy to add a folder to Android when running the program. Everything is correct and it does not give any error but nothing is added.
here is my MainActivity
code
package com.example.use_chaquopy;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;
public class MainActivity extends AppCompatActivity {
Context context = this;
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView t1 = findViewById(R.id.t);
if (! Python.isStarted()) {
Python.start(new AndroidPlatform(context));
}
if (Python.isStarted()) {
t1.setText("python is start");
}
}
}
and this id python
script
import os
os.mkdir("loolmkkukj")
What's wrong?
CodePudding user response:
Your Java code isn't actually running any Python script. Check the Chaquopy documentation for examples of how to do that.
Also, your script won't work, because on Android, the current directory is usually the root directory, which is unwritable by normal apps. Instead, as described here, you should write to os.environ["HOME"]
, which is set to your app's internal storage directory.