You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Google Apps Script保存Android上传图片无法打开的问题

问题:Android上传图片至Google Apps Script后无法正常打开

我需要通过Android应用将图片上传到Google Apps Script,并把相关数据追加到Google Sheet中。目前基础通信功能正常,Android应用可以调用doPost方法传递文本数据,但上传的图片保存到Google Drive后无法打开——文件类型显示为正确的图片格式,但无论是在Drive内预览还是下载到本地PC后都无法正常打开。

当前Google Apps Script的doPost代码

function doPost(e) {
  var action = e.parameter.action;
  //  if(action == "postFood"){
    try {

      var contentType = "image/jpeg";
      var contents = e.postData.contents; // 我试过用Utilities.base64decode,但没用
      var fileName = Utilities.formatDate(new Date(), 'GMT', 'yyyy-MM-dd h:mm a');
      
      var imageBlob = Utilities.newBlob(contents, contentType, fileName);

      return handleImageAndAppendRow(e.parameter, imageBlob);
    } catch (error) {
      return ContentService.createTextOutput("{message:'Error', details:'" + error + "'}").setMimeType(ContentService.MimeType.JSON);
    }
  // }
}

可行的测试方法

我写了一个测试方法,通过下载图片而非上传的方式调用handleImageAndAppendRow()可以成功运行,图片能正常保存并打开:

function downloadTestPic(){
  var contentType = "image/jpeg";
  var options = {
      "method" : "get",
      "contentType": contentType,
      'muteHttpExceptions': true
  };

  var url = "https://www.wksusa.com/wp-content/uploads/2022/07/IMG-0423-scaled-e1658446014847-610x350.jpeg";
  var response = UrlFetchApp.fetch(url, options);
  
  var blob = response.getBlob();
  blob.setContentTypeFromExtension();
  
var sampleData = {
    'date': '1/4/2024',
    'meal': 'Lunch',
    'description': 'Chipotle'
  }
  
handleImageAndAppendRow(sampleData, blob)
}

Android端代码

我已经确认Android应用向其他服务上传图片完全正常,问题出在Google Apps Script对图片数据的处理上:

private fun run(foodDescription: String, selectedDate: String, selectedMeal: String, imageFile: File) {
        println("Saving $imageFile.name")

        val requestBody = MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("action", "postFood")
            .addFormDataPart("date", selectedDate)
            .addFormDataPart("meal", selectedMeal)
            .addFormDataPart("description", foodDescription)
            .addFormDataPart("image", imageFile.name, imageFile.asRequestBody(MEDIA_TYPE_JPEG))
            .build()


//        val request = Request.Builder() // 这段代码可以正常上传到其他服务!
//            .header("Authorization", "Client-ID 9199fdef135c122")
//            .url("https://api.imgur.com/3/image")
//            .post(requestBody)
//            .build()
        
        val request = Request.Builder()
            .url("https://script.google.com/macros/s/AKfycbwQ1nLEzgMwHsa3iSovL5zNpZG9jE-T4_Bsg_yQs0E7pc6tpscvKc-4oakOCZMyB4vk/exec")
            .post(imageFile.asRequestBody(MEDIA_TYPE_JPEG))
//            .post(requestBody)
            .build()

        client.newCall(request).execute().use { response ->
            if (!response.isSuccessful) throw IOException("Unexpected code $response")
            Log.d("API lame", response.body!!.string());
            println(response.body!!.string())
        }
    }

恳请针对Google Apps Script的图片数据处理部分提供解决建议。

内容的提问来源于stack exchange,提问作者Trevor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 00:16:08