发布在android中提供意外值的doingbackground的进度

发布在android中提供意外值的doingbackground的进度,android,Android,当我在doingbackground中发布进度时,它会在进行中更新中为我提供意外的值。发布后,进度条有时甚至可以达到数千条 这是我的密码 公共静态类FileServerAsyncTask扩展了AsyncTask{ 私人语境; 公共文件服务器异步任务(上下文){ this.context=上下文; } @凌驾 受保护字符串doInBackground(无效…参数){ 试一试{ //插座 ServerSocket ServerSocket=新的ServerSocket(8988); Log.d(Fi

当我在doingbackground中发布进度时,它会在进行中更新中为我提供意外的值。发布后,进度条有时甚至可以达到数千条

这是我的密码

公共静态类FileServerAsyncTask扩展了AsyncTask{
私人语境;
公共文件服务器异步任务(上下文){
this.context=上下文;
}
@凌驾
受保护字符串doInBackground(无效…参数){
试一试{
//插座
ServerSocket ServerSocket=新的ServerSocket(8988);
Log.d(Find_People_Activity.TAG,“Server:socketopened”);
Socket client=serverSocket.accept();
//输入流
InputStream InputStream=client.getInputStream();
//数据输入流
DataInputStream DataInputStream=新的DataInputStream(inputstream);
//长度和.ext(mp4)
long length=dataInputStream.readLong();
字符串扩展名=dataInputStream.readUTF();
//要保存的文件路径
Log.d(Find_People_Activity.TAG,“Server:connection done”);
final File f=新文件(context.getExternalFilesDir(“已接收”),
“dropit-”+System.currentTimeMillis()+扩展名
);
File dirs=新文件(f.getName());
如果(!dirs.exists())
dirs.mkdirs();
f、 createNewFile();
Log.d(Find_People_Activity.TAG,“server:copying files”+f.toString());
FileOutputStream out=新的FileOutputStream(f);
字节buf[]=新字节[1024];
内伦;
int-total=0;
试一试{
而((len=inputstream.read(buf))!=-1){
out.write(buf,0,len);
out.flush();
总+=len;
这里是问题>>出版进度((int)((总计*100)/长度));
}
dataInputStream.close();
out.close();
inputstream.close();
}捕获(IOE异常){
Log.d(Find_People_Activity.TAG,例如toString());
}
serverSocket.close();
返回f.getAbsolutePath();
}捕获(IOE异常){
Log.e(Find_People_Activity.TAG,e.getMessage());
返回null;
}
}
@凌驾
受保护的void onPostExecute(字符串结果){
如果(结果!=null){
Toast.makeText(上下文,“文件复制到:”+result,Toast.LENGTH_SHORT.show();
File recvFile=新文件(结果);
Uri fileUri=FileProvider.getUriForFile(
上下文
“com.example.dropit”,
记录文件);
}
}
@凌驾
受保护的void onPreExecute(){
Toast.makeText(上下文,“等待文件”,Toast.LENGTH_SHORT.show();
}
@凌驾
受保护的void onProgressUpdate(整型…值){
super.onProgressUpdate(值);
progressBar.setProgress((值[0]);
百分比.setText(“加载…”+值[0]+“%”);
}
}

当我发布进度时,我得到的文件长度是以kbs为单位的。从数据输入流中,文件长度约为910000kb

 public static class FileServerAsyncTask extends AsyncTask<Void, Integer, String> {

    private Context context;


    public FileServerAsyncTask(Context context) {
        this.context = context;


    }

    @Override
    protected String doInBackground(Void... params) {

        try {
            //socket
            ServerSocket serverSocket = new ServerSocket(8988);
            Log.d(Find_People_Activity.TAG, "Server: Socket opened");
            Socket client = serverSocket.accept();
            //inputstream
            InputStream inputstream = client.getInputStream();
            //datainputstream
            DataInputStream dataInputStream=new DataInputStream(inputstream);
            //length and .ext(mp4)
            long length=dataInputStream.readLong();
            String extension=dataInputStream.readUTF();
            //filepath to be saved
            Log.d(Find_People_Activity.TAG, "Server: connection done");
            final File f = new File(context.getExternalFilesDir("received"),
                    "dropit-" + System.currentTimeMillis() +extension
            );
            File dirs = new File(f.getName());


            if (!dirs.exists())
                dirs.mkdirs();
            f.createNewFile();
            Log.d(Find_People_Activity.TAG, "server: copying files " + f.toString());
            FileOutputStream out =new FileOutputStream(f);



            byte buf[] = new byte[1024];
            int len;
            int total = 0;
            try {

                while ((len = inputstream.read(buf)) != -1) {


                    out.write(buf, 0, len);
                    out.flush();
                    total += len;
          here is problem >> publishProgress((int)( (total * 100) /length));

                }
                dataInputStream.close();
                out.close();
                inputstream.close();
            } catch (IOException e) {
                Log.d(Find_People_Activity.TAG, e.toString());

            }


            serverSocket.close();
            return f.getAbsolutePath();
        } catch (IOException e) {
            Log.e(Find_People_Activity.TAG, e.getMessage());
            return null;
        }

    }

    @Override
    protected void onPostExecute(String result) {
        if (result != null) {

            Toast.makeText(context, "File Copied to :" + result, Toast.LENGTH_SHORT).show();

            File recvFile = new File(result);
            Uri fileUri = FileProvider.getUriForFile(
                    context,
                    "com.example.dropit",
                    recvFile);


        }

    }

    @Override
    protected void onPreExecute() {
        Toast.makeText(context, "waiting for files", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        progressBar.setProgress((values[0]));
        percentage.setText("Loading..." + values[0] + "%");
    }
}