Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用进度条更新文件上载在android中不起作用_Android_File Upload_Progress Bar_Retrofit_Percentage - Fatal编程技术网

用进度条更新文件上载在android中不起作用

用进度条更新文件上载在android中不起作用,android,file-upload,progress-bar,retrofit,percentage,Android,File Upload,Progress Bar,Retrofit,Percentage,在我的应用程序中,我使用改型将文件上载到服务器。现在我想显示进度条中文件上载的百分比。我从中找到了一个解决办法 但是,对于每个不同大小的文件,进度条都会快速填充。我认为这个进度不是上传到服务器的实际数据量。对于这个问题,在改造中是否有其他解决方案。。?我也和你核对过了 是否有可能进行改装 请看我的密码 这是自定义类型的文件 public class CountingTypedFile extends TypedFile { private static final int BUFFER

在我的应用程序中,我使用改型将文件上载到服务器。现在我想显示进度条中文件上载的百分比。我从中找到了一个解决办法

但是,对于每个不同大小的文件,进度条都会快速填充。我认为这个进度不是上传到服务器的实际数据量。对于这个问题,在改造中是否有其他解决方案。。?我也和你核对过了

是否有可能进行改装

请看我的密码

这是自定义类型的文件

public class CountingTypedFile extends TypedFile {

    private static final int BUFFER_SIZE = 4096;

    private final ProgressListener listener;

    public CountingTypedFile(String mimeType, File file, ProgressListener listener) {
        super(mimeType, file);
        this.listener = listener;
    }

    @Override
    public void writeTo(OutputStream out) throws IOException {
        byte[] buffer = new byte[BUFFER_SIZE];
        FileInputStream in = new FileInputStream(super.file());
        long total = 0;
        try {
            int read;
            while ((read = in.read(buffer)) != -1) {
                total += read;
                out.flush();
                this.listener.transferred(total);
                out.write(buffer, 0, read);
            }
        } finally {
            in.close();
        }
    }
}
这是我在AsyncTask的doingbackground中执行的代码部分

Uri myUri = Uri.parse(mImgUri);
            File file = new File(AppUtils.getRealPathFromURI(context, myUri));
            totalSize = file.length();
            RestAdapter restAdapter = new RestAdapter.Builder()
                    .setEndpoint(Constants.BASE_URL)
                    .setLogLevel(RestAdapter.LogLevel.BASIC)
                    .setClient(new OkClient(new OkHttpClient()))
                    .build();
            RetrofitCommonService mRetrofitCommonService = restAdapter.create(RetrofitCommonService.class);
            Log.d("TAG", "File is not null when converting from bitmap");

            TypedFile fileToSend  = new CountingTypedFile("image/png", file, new ProgressListener() {
                @Override
                public void transferred(long num) {
                    publishProgress((int) ((num / (float) totalSize) * 100));
                }
            });

            ImageUploadResponse imageUploadResponse = mRetrofitCommonService.upLoadImage(new AppUtils(context).getPhone(),
                    AppUtils.getDeviceId(context), fileToSend);
这是我的改装服务

@Multipart
@POST("/user/asset")
@Headers("Accept:application/json")
ImageUploadResponse upLoadImage(@Header("mobile-number") String mPhone, @Header("uid") String imei,
                                @Part("file") TypedFile mBody);

请添加您的代码…我面临着同样的问题,看来一个人必须推出自己的客户可能是通过扩展OKHttp@Nidheesh你们能解决这个问题吗?我也有同样的问题