我正在寻找一种将多张图片按顺序上传到服务器的实现策略。我正在使用来自 http://uploads.im 的 API上传我的图片。它不允许在一个多部分 POST 请求中选择多个图像。因此我必须按顺序上传图像。
用户会“点击”多张照片,这必须上传到 http://uploads.im .一旦我从 API 获得带有上传图片 url 的成功响应,我就需要上传另一个,依此类推。此外,一旦我将所有图像上传到图像服务器,我需要将 URL 列表发送到我的服务器,服务器存储这些详细信息。
这是我到目前为止所做的
@OnClick({R.id.pic_photo_0
, R.id.pic_photo_1
, R.id.pic_photo_2
, R.id.pic_photo_3
}) //Bind with butterknife
public void onClickPhotoFrame(View view)
{
// Start CameraActivity
Intent startCustomCameraIntent = new Intent(this, CameraActivity.class);
startActivityForResult(startCustomCameraIntent, REQUEST_CAMERA);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode != RESULT_OK)
{
return;
}
if (requestCode == REQUEST_CAMERA)
{
Uri photoUri = data.getData();
//based on which imageview created the camera request, load it with the above image
Picasso.with(getApplicationContext()).load(photoUri).fit().into(imageView0);
}
}
1)在上面的代码中是否可以知道哪个view调用了onActivityResult方法?基于此,我可以使用相机的缩略图设置 ImageView 。
2) 我还将相机返回的 uri 添加到 arraylist 中,稍后可以使用它来循环和上传图像。如果这种方法可行,请告诉我。
我正在通过改造上传如下图片
for (int i = 0; i < imageStorageList.size(); i++)
{
Call<ImageUploadResponse> call = new ImageUploadHelper().
UploadImage(new File(imageStorageList.get(i).getPath()));
call.enqueue(new Callback<ImageUploadResponse>()
{
@Override
public void onResponse(Call<ImageUploadResponse> call, Response<ImageUploadResponse> response)
{
// do some magic here to know which image was uploaded
}
@Override
public void onFailure(Call<ImageUploadResponse> call, Throwable t)
{
}
});
}
3)我很确定上面的策略是不行的。请帮助我如何实现这一点。
最佳答案
1) In the above code is it possible to know which view called the onActivityResult method? Based on that I can set the imageview with the thumbnail from camera.
没有标准的方法可以知道哪个 View
启动了相机 Intent
。如果你真的需要知道它,你可以创建局部变量:
private ImageView lastClickedView;
然后在 onClickPhotoFrame
中执行此操作:
public void onClickPhotoFrame(View view){
lastClickedView = (ImageView) view;
// Start CameraActivity
Intent startCustomCameraIntent = new Intent(this, CameraActivity.class);
startActivityForResult(startCustomCameraIntent, REQUEST_CAMERA);
}
并在 onActivityResult
中添加对这种情况的处理:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode != RESULT_OK)
{
return;
}
if (requestCode == REQUEST_CAMERA)
{
Uri photoUri = data.getData();
//based on which imageview created the camera request, load it with the above image
Picasso.with(getApplicationContext()).load(photoUri).fit().into(lastClickedView);
}
}
2) I am also adding the uri returned by the camera to a arraylist, which can be used later to loop and upload the image. Please let me know if this approach is OK.
在我看来,你的做法是可以的。但我看到了你的另一个答案:Call same retrofit method sequentially for result using RxJava
据我了解,您想使用 RxJava 来完成。
因此,这是一种可能的方法(从您的答案中复制并稍作更改):
Observable.from(imageStorageList)
.flatMap(uri -> new ImageUploadHelper().uploadImage(new File(uri.getPath())))
.subscribe(imageUploadResponseObservable -> {
}, throwable -> {/*handle it here*/});
优点:
- 实现简单
缺点:
- 实际上这个变体不处理错误。我的意思是,如果出现错误,您需要尝试再次上传图片。
如何解决这个问题?
I. 如果没有 RxJava,就像在工作上一样看待这个。并使用图书馆 android-priority-jobqueue .这些作业将持久,并且在错误情况下,它们将以指数退避的方式重复。
II. 实现您自己的机制,该机制将在服务器上上传图片并持久保存。
关于android - 通过改造按顺序将图像上传到 REST API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36813794/