1

I want to integrate python with my android app. What I want is: I will write a python script and I will put it somewhere in my project folder. And I should be able to call that script from my activity class .java file. In simple words, I want to build my complete app in Android Studio only but I want to use python for some parts of my code.

I know of only one thing that perfectly fits this criteria and that's "Chaquopy" but I don't want to use Chaquopy.

Can you please suggest something else? Thank you

2
  • 1
    What is it about Chaquopy that doesn't suit your requirements? It would probably help to understand your requirements. Commented Jun 18, 2020 at 3:16
  • Chaquopy is the best suited for my case @Ryan M but I have to buy a license for using it and I just develop apps for fun, free of cost. So I want an alternative. Commented Jun 18, 2020 at 10:32

1 Answer 1

1

I would use flask and have an endpoint like so:

@app.rooute('/data', methods=['GET'])
def meth():
   # python code here
   return make_response(jsonify({'results': ret}), 200)

I've actually set up an endpoint for you to use here that takes a png file, uses pillow to resize it to 1200 pixels and returns the new png.

Your task is now to display the PNG however you would in Java.

EDIT: There are many, many approaches to read data from the HTTP endpoint in Java, one is given below, using okhttp:

Request request = new Request.Builder().url("https://hd1-martin.herokuapp.com/data").build();
Response rawResponse = new OkHttpClient().newCall(request).execute();
byte[] response = rawResponse.body().bytes();

This is the limit of my expertise, I'm now leaving the rest in your capable hands to get the new image to display in Android, with the following hint... I suspect you're going to be looking at writing the bytes -- in response in the snippet -- to a file and loading it into an Android ImageView.

Hope that helps.

Sign up to request clarification or add additional context in comments.

23 Comments

would you be kind enough to tell me this in detail. I don't have even a vaguest idea about this. Can you tell me all steps in detail. Like what line should I add in build.gradle, what to import, where to put the code. Thank you.
How can I integrate it with Android Studio so that I can call the python method from anywhere in between my code. It should be there when I complete the app build and export it as an apk
Separate from android. Then use OkHttp to make the call. Should you need further assistance, let's get a chatroom started and handle it there
I am unable to chat there. It says you must have 20 reputation to chat here.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.