将多个映像上传到队列中的服务器

Ami*_*ari 13 android file-upload android-service retrofit

使用案例:将后台队列中的图像上传到服务器,图像可以是存储在手机内存中的网址或图像文件.

我想要的是将队列中的项目数量限制为3并将模糊图像显示为在活动中的回收站视图中上载的实际图像的占位符,每个占位符上都有一个进度条指示已上载了多少内容.在每个占位符的顶部有三个按钮,可以暂停,取消或恢复上传图像.

现状:现在,我是用MultipartRetrofit 1.9.0上传图片和该服务呼叫在活动中完成的.

我无法弄清楚如何使用Retrofit或任何其他库来取消,暂停或恢复多部分POST请求以及如何将UI事件与api服务线程联系起来.我可以从服务更新UI,但是如何从UI中的事件(暂停/恢复/取消)更新服务中的某些内容?

我该如何处理这个用例?我需要使用服务吗?我可以根据服务中执行的请求在其他活动中显示进度指示器吗?这个过程的架构应该是什么?我不需要它的代码,但是如果有一些与此相关的有用的引用,我想阅读并测试它以最终得出我的方法.

San*_*shi 1

您可以这样做:这是通过传递图像路径从设备上传图像的示例代码。类似地,您可以对图像 url 执行此操作,步骤:1) 创建一个将为每个图像运行的线程。2)之后每张图片上传都会给你回复。3) 现在,对于每个图像,您可以更新您的 UI。

//TODO: Multiple file upload
public class FileUpload implements Runnable {
    Context context;
    String uploadApiUrl, uploadFilePath, fileType;
    int uploadId;
    LocalQueenDataBase localQueenDataBase;
    Activity activity;

    public FileUpload(Context context, ,String uploadApiUrl, String uploadFilePath, String fileType, int uploadId) {
        this.context = context;
        this.uploadApiUrl = uploadApiUrl;
        this.uploadFilePath = uploadFilePath;
        this.fileType = fileType;
        this.uploadId = uploadId;
        localQueenDataBase = new LocalQueenDataBase(context);
        Thread uploader = new Thread(this);
        uploader.start();
    }

    @Override
    public void run() {
        try {
            executeMultipartPost();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void executeMultipartPost() throws Exception {
        try {
            String originalPath = uploadFilePath;
            if (uploadFilePath == null) {
                uploadFilePath = originalPath;
            }
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("your api url");
            httppost.addHeader("put your header if required")
            FileBody bin = new FileBody(new File(uploadFilePath));
            StringBody fileTypeBody = new StringBody(fileType);
            StringBody uploadIdBody = new StringBody(uploadId + "");

            MultipartEntity reqEntity = new MultipartEntity();
            reqEntity.addPart("file", bin);
            reqEntity.addPart("fileType", fileTypeBody);
            reqEntity.addPart("uploadId", uploadIdBody);
            httppost.setEntity(reqEntity);

            HttpResponse response = httpclient.execute(httppost);
            HttpEntity resEntity = response.getEntity();
            String retSrc = EntityUtils.toString(resEntity);//Render your response
            //TODO: update your UI for each uploaded image by creating the Handler
             Message msg = handler.obtainMessage();
             handler.sendMessage(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

final Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        try {
            uploadGalleryRecyclerAdapter = new UploadGalleryRecyclerAdapter(getApplicationContext(), PostUpdateActivity.this, localQueenDataBase.getGallery());
            postUpdateRV.setAdapter(uploadGalleryRecyclerAdapter);
            if (localQueenDataBase.getGallery().size() > 3)
                gallerylayoutManager.scrollToPosition(localQueenDataBase.getGallery().size() - 2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.handleMessage(msg);
    }
};
Run Code Online (Sandbox Code Playgroud)

希望对你有帮助。