0

I am really new to programming Flask APIs. Is there any way to send a audio file which is recorded on an android device to my flask server? Should I send audio file byte by byte or is there a way to send it directly ?

0

1 Answer 1

1

Save the audio and use multipart

In your flask script

@app.route('/uploadfile',methods=['GET','POST'])
def uploadfile():
    if request.method == 'PUT':
        f = request.files['file']
        filePath = "./somedir/"+secure_filename(f.filename)
        f.save(filePath)
        return "success"

In your android app

public static void upload(String path) throws IOException{

    OkHttpClient client = new OkHttpClient.Builder().connectTimeout(30, TimeUnit.SECONDS).writeTimeout(30, TimeUnit.SECONDS).readTimeout(30, TimeUnit.SECONDS).build();
    MultipartBody.Builder mMultipartBody = new MultipartBody.Builder().setType(MultipartBody.FORM).
        mMultipartBody.addFormDataPart("file","upload",new CountingFileRequestBody(new File(path),"*/*", null));


    RequestBody mRequestBody = mMultipartBody.build();
    Request request = new Request.Builder()
            .url("yourUrl/uploadfile").post(mRequestBody)
            .build();

    Response response = client.newCall(request).execute();
    String responseString = response.body().string();

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.