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 ?

inovasyon
  • 9
  • 1
  • 3

1 Answers1

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();

}
Niza Siwale
  • 2,390
  • 1
  • 18
  • 20