我正在尝试跟踪压缩进度. ATM我正在这样做:
public static voID compressGzipTest(final OutputStream os, final file source) throws CompressorException, IOException { final CountinginputStream cis = new CountinginputStream(new fileinputStream(source)); final GzipCompressorOutputStream gzipOut = (GzipCompressorOutputStream) new CompressorStreamFactory() .createCompressorOutputStream(CompressorStreamFactory.GZIP,os); new Thread() { public voID run() { try { long fileSize = source.length(); while (fileSize > cis.getBytesRead()) { Thread.sleep(1000); System.out.println(cis.getBytesRead() / (fileSize / 100.0)); } } catch (Exception ex) { ex.printstacktrace(); } } }.start(); IoUtils.copy(cis,gzipOut); }
这可以正常工作,但是我需要线程,该线程给出的进度反馈不是在此方法中实现的,而是在调用它时(为了在androID设备上创建进度条之类的东西).因此,这更像是一个体系结构问题.有什么想法,如何解决?
解决方法:
同时,我通过添加接口作为参数来覆盖IoUtils.copy()来解决此问题:
public static long copy(final inputStream input, final OutputStream output, int buffersize, ProgressListener Listener) throws IOException{ final byte[] buffer = new byte[buffersize]; int n = 0; long count = 0; while (-1 != (n = input.read(buffer))) { output.write(buffer,0,n); count += n; Listener.onProgress(n); } return count;}
然后被这样的东西调用
copy(input, output, 4096, new ProgressListener() { long totalCounter = 0; DecimalFormat f = new DecimalFormat("#0.00"); @OverrIDe public voID onProgress(long bytesRead) { totalCounter += bytesRead; System.out.println(f.format(totalCounter / (fileSize / 100.0))); } });
到目前为止,我面临的唯一挑战是限制控制台上的输出不是针对每个字节[4096],而是针对每个2兆字节.我尝试过这样的事情:
while (-1 != (n = input.read(buffer))) { output.write(buffer,0,n); count += n; while(n % 2097152 == 0) { Listener.onProgress(n); } } return count;
但这根本不给我任何输出
总结以上是内存溢出为你收集整理的android-跟踪复制文件的进度全部内容,希望文章能够帮你解决android-跟踪复制文件的进度所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)