android - 如何在 Android 中将文件上传到 GCS 时显示进度

标签 android google-cloud-storage progress resume pause

我试过了 this在 android 中的 GCS 上上传文件的代码。此代码运行完美并在存储桶上上传文件。 现在我想通过暂停、恢复和互联网开/关处理来检查进度。 我在互联网上搜索了很多,但没有找到任何完美的解决方案。

代码

CloudStorage.uploadFile("upload-recording", "/storage/sdcard0/Download/images.jpg",this,file);

public static void uploadFile(String bucketName, String filePath,Context context,File file_use)
            throws Exception {

        this_context = context;
        this_file = file_use;
        Storage storage = getStorage();

        StorageObject object = new StorageObject();
        object.setBucket(bucketName);

        File file = new File(filePath);

        InputStream stream = new FileInputStream(file);
        try {
            String contentType = URLConnection
                    .guessContentTypeFromStream(stream);
            InputStreamContent content = new InputStreamContent(contentType,
                    stream);

            Storage.Objects.Insert insert = storage.objects().insert(
                    bucketName, null, content);
            insert.setName(file.getName());

            insert.execute();
        } finally {
            stream.close();
        }
    }

最佳答案

尝试使用 CustomInputStream 获取数据读取状态并使用服务显示它。

public class ProgressNotifyingInputStream extends InputStream {
    private InputStream wrappedStream;
    private int count = 0;
    private int totalSize;
    File file;
    public ProgressNotifyingInputStream(File file)
     {
       this.file=file;
      }
    /**
     * Creates a new notifying outputstream which wraps an existing one.
     * When you write to this stream the callback will be notified each time when
     * updateAfterNumberBytes is written.
     * 
     * @param stream the outputstream to be wrapped
     * @param totalSize the totalsize that will get written to the stream
     */



     wrappedStream = new FileInputStream(file);

    public ProgressNotifyingInputStream(InputStream stream, int totalSize) {
        if(stream==null) {
            throw new NullPointerException();
        }
        if(totalSize == 0) {
            throw new IllegalArgumentException("totalSize argument cannot be zero");
        }
        this.wrappedStream = stream;
        this.totalSize = totalSize;
    }


    @Override
    public int read() throws IOException {
        count++;
        return wrappedStream.read();
    }

    /**
     * Get progress from 0 to 100
     * @return
     */
    public int getProgress() {
        return count * 100 / totalSize;
    }

}

我的代码只是调用

InputStream stream=new ProgressNotifyingInputStream (file);

关于android - 如何在 Android 中将文件上传到 GCS 时显示进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32666658/

相关文章:

java - 单击/从列表首选项的键更改项目

标记旋转后的Android Google Map InfoWindow anchor

java - StorageOptions 类型的 getService() 方法未定义

node.js - 将流从 Fluent-ffmpeg 传递到 Google Cloud Storage

java - com.google.cloud.storage.StorageException : 401 Unauthorized

windows - 有没有办法在 Perl 脚本中使用移动函数来获取文件传输的进度?

android - fragment 未显示在 Activity 顶部

Android 创建具有 3 个圆角和 1 个直角的 ImageView

mysql - SQL查看查询进度

java - 如何在android中启动和停止进度条?