这是你可以做的:这是通过传递图像的路径从设备上传图像的示例代码.类似地,您可以为

Image 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);

}

};

希望这将有助于您.

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐