上传音频文件

直接上代码


public class MainActivity extends AppCompatActivity {

    private TextView tv;
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initPers();

        initView();
    }


    private void initView() {
        tv = findViewById(R.id.tv);
        btn = findViewById(R.id.btn);

       //点击按钮后开始上传
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                upload();
            }
        });
    }

    //动态授权
    private void initPers() {

        String[] pers = {
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
        };
        ActivityCompat.requestPermissions(this, pers, 1);
    }


    public void upload() {
        File file1 = new File(Environment.getExternalStorageDirectory() + "/com.mypro/" + "aaa.mp3"); //本地音频文件

        if (file1 == null) {
            //文件不存在
            return;
        }

        final String url = "https://api.steponeai.com/api/v1/upload";

        // File file = new File(Environment.getExternalStorageDirectory() + "/1560237648804.amr"); //本地音频文件

        //if (bool) {
        //如果文件存在
        OkHttpClient okHttpClient = new OkHttpClient.Builder().build(); //创建Okhttpclient对象
        //创建requestBody对象  封装文件格式
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/octet-stream"), file1);


        MultipartBody multipartBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("company_id", company_id, requestBody)//第一个参数为后台定好的字段名称
                .addFormDataPart("user_token", user_token)
                .addFormDataPart("file", file1.toString)//如果还需传递其他字段调用此方法传递
                .build();

        //创建request对象
        Request request = new Request.Builder().url(url).post(multipartBody).build();

        okHttpClient.newCall(request)
                .enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e("failed", e.getMessage());
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        final String string = response.body().string();
                        Log.e("shangchuan", string);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                tv.setText(string); 
                            }
                        });
                    }
                });

    }



}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值