Browse Source

feat 上传图片

lihao1 3 years ago
parent
commit
8f15bc5c4a

+ 6 - 0
demo/src/main/java/com/framework/mvvm/mv/AdmViewModel.kt

@@ -24,6 +24,8 @@ import com.framework.mvvm.model.vo.Building
 import com.framework.mvvm.model.vo.Client
 import com.framework.mvvm.model.vo.Login
 import com.sybotan.android.demo.DemoApp
+import com.sybotan.android.demo.tools.LocalDataOperation
+import com.yuyh.library.imgsel.utils.LogUtils
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.async
 import kotlinx.coroutines.flow.*
@@ -250,6 +252,10 @@ class AdmViewModel(private val repository: AdmRepository) : ViewModel() {
                     val result = uploadFile(url, body)
 
                     Log.d("fileUpload", "success: $result")
+                    if ("success" == result) {
+                        //不管是那种情况,只要成功都需要从本地删除该图片¬
+                        file.renameTo(File(LocalDataOperation.getInstance().getOfflineImageFilePath(DemoApp.mContext) + File.separator + "uploaded" + file.name))
+                    }
                 }
             } else {
                 Log.d("fileUpload", "upload file has something error:$response")

+ 1 - 1
demo/src/main/java/com/sybotan/android/demo/activities/PocActivity.kt

@@ -135,7 +135,7 @@ class PocActivity : BaseActivity(), DIAware {
 
         uploadBtn.setOnClickListener {
             lifecycleScope.launch(Dispatchers.IO) {
-                LocalDataOperation.getInstance().offlineSendRequest.uploadImageNew(this@PocActivity)
+                LocalDataOperation.getInstance().offlineSendRequest.uploadImageNew(this@PocActivity, mVm, projectId, groupCode)
             }
             mVm.uploadBuilding(
                 request, block = {

+ 34 - 24
demo/src/main/java/com/sybotan/android/demo/tools/OfflineSendRequest.java

@@ -4,6 +4,7 @@ import android.content.Context;
 import android.text.TextUtils;
 import android.util.Log;
 
+import com.framework.mvvm.mv.AdmViewModel;
 import com.google.gson.Gson;
 import com.sybotan.android.demo.retrofit.Constant;
 import com.yuyh.library.imgsel.utils.LogUtils;
@@ -12,6 +13,8 @@ import org.json.JSONException;
 import org.json.JSONObject;
 
 import java.io.File;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * @author wangjie
@@ -31,37 +34,44 @@ public class OfflineSendRequest {
      * 新的离线图片存储上传
      *
      * @param context
+     * @param mVm
      */
-    public void uploadImageNew(Context context) {
+    public void uploadImageNew(Context context, AdmViewModel mVm, String projectId, String groupCode) {
         String imageFilePath = LocalDataOperation.getInstance().getOfflineImageFilePath(context);
         File imageFile = new File(imageFilePath);
         if (imageFile.exists()) {
             File[] files = imageFile.listFiles();
             if (files != null && files.length > 0) {
                 for (File file : files) {
-                    if (!TextUtils.isEmpty(file.getName()) && !file.getName().startsWith("uploaded")) {
-                        final String url = Constant.ServerImgIPUp + file.getName();
-                        Log.e("uploadImageNew url", url);
-                        String result = UploadImage.uploadFiles(file, url);
-                        if (!TextUtils.isEmpty(result)) {
-                            String resultString = null;
-                            try {
-                                JSONObject object = new JSONObject(result);
-                                resultString = object.getString("Result");
-                                Log.e("resultString", resultString);
-                            } catch (JSONException e) {
-                                e.printStackTrace();
-                            }
-                            if ("success".equals(resultString)) {
-                                //不管是那种情况,只要成功都需要从本地删除该图片
-                                boolean b = file.renameTo(new File(imageFilePath + File.separator + "uploaded" + file.getName()));
-                                //file.delete();
-                                LogUtils.e("uploadImageNew", "上传成功" + b + file.getName());
-                            } else {
-                                LogUtils.e("uploadImageNew", "上传失败");
-                            }
-                        }
-                    }
+                    Map param = new HashMap<String, String>();
+                    param.put("appId", "adm");
+                    param.put("userId", "bdtp");
+                    param.put("groupCode", groupCode);
+                    param.put("projectId", projectId);
+                    mVm.fileUpload(param, file);
+//                    if (!TextUtils.isEmpty(file.getName()) && !file.getName().startsWith("uploaded")) {
+//                        final String url = Constant.ServerImgIPUp + file.getName();
+//                        Log.e("uploadImageNew url", url);
+//                        String result = UploadImage.uploadFiles(file, url);
+//                        if (!TextUtils.isEmpty(result)) {
+//                            String resultString = null;
+//                            try {
+//                                JSONObject object = new JSONObject(result);
+//                                resultString = object.getString("Result");
+//                                Log.e("resultString", resultString);
+//                            } catch (JSONException e) {
+//                                e.printStackTrace();
+//                            }
+//                            if ("success".equals(resultString)) {
+//                                //不管是那种情况,只要成功都需要从本地删除该图片
+//                                boolean b = file.renameTo(new File(imageFilePath + File.separator + "uploaded" + file.getName()));
+//                                //file.delete();
+//                                LogUtils.e("uploadImageNew", "上传成功" + b + file.getName());
+//                            } else {
+//                                LogUtils.e("uploadImageNew", "上传失败");
+//                            }
+//                        }
+//                    }
                 }
             }
         }