【问题标题】:may I set file size when user click the submit upload server before?当用户点击提交上传服务器之前,我可以设置文件大小吗?
【发布时间】:2021-03-05 17:32:08
【问题描述】:

我可以在用户点击提交上传服务器之前设置文件大小吗?

如果视频文件大小超过10MB,则文件无法选择或无法上传,怎么办?

这是我的代码

private void selectVideo() {
        Intent intent = new Intent();
        intent.setType("video/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, VIDEO);
    }

if(requestCode == VIDEO){
                image.setVisibility(View.GONE);
                previewImageView.setVisibility(View.VISIBLE);

                //video.setVisibility(View.VISIBLE);
                contentURI = data.getData();
                videoPath = getFilePathFromContentUri(contentURI, getContentResolver());
                Glide.with(this).load(contentURI).into(previewImageView);
                video.setVideoURI(contentURI);
                video.requestFocus();
                //video.start();
                MediaController mc= new MediaController(UploadActivity.this);
                video.setMediaController(mc);
                check_video = "ok";
            }

private void uploadFile() {
        progressDialog.show();

Map<String, RequestBody> map = new HashMap<>();
            //File file = new File(newvideoPath);
            String fullFilePath = PathUtil.getPathFromUri(this,contentURI);
            // Parsing any Media type file
            File file = new File(fullFilePath);
            String filename = getID+ "_"+ fullFilePath.substring(fullFilePath.lastIndexOf("/")+1);

            RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), file);
            map.put("file\"; filename=\"" + file.getName() + "\"", requestBody);
            ApiConfig getResponse = AppConfig.getApiClient().create(ApiConfig.class);
            Call<ServerResponse> call = getResponse.upload("token", map,imageName,userID,sp2,tag);
            call.enqueue(new Callback<ServerResponse>() {
                @Override
                public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
                    if (response.isSuccessful()){
                        if (response.body() != null){
                            progressDialog.dismiss();
                            ServerResponse serverResponse = response.body();
                            Toast.makeText(getApplicationContext(), serverResponse.getMessage(), Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(UploadActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    }else {

                        Toast.makeText(getApplicationContext(), "problem uploading video", Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFailure(Call<ServerResponse> call, Throwable t) {

                    Log.v("Response gotten is", t.getMessage());
                    Toast.makeText(getApplicationContext(), "problem uploading video " + t.getMessage(), Toast.LENGTH_SHORT).show();

                }
            });

我试过在php上设置,但这不是我想要的,我希望我可以在uploadActivity上设置。如果文件大小超过 10MB 则用户无法选择文件

【问题讨论】:

    标签: php android upload retrofit


    【解决方案1】:

    以下是了解文件大小的方法,然后您就可以使用它了。

    File file = new File(fullFilePath);
    long length = file.length();
    length = length/1024;
    Toast.makeText(getActivity(), "Video size:"+length+"KB",
    Toast.LENGTH_LONG).show();
    

    【讨论】:

    • 大家好,你们知道如何压缩视频吗?我的意思是,当用户上传视频时,如何压缩和降低视频质量?我只知道图像方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 2021-10-29
    • 1970-01-01
    • 2014-01-13
    相关资源
    最近更新 更多