dongliu8542 2014-11-29 07:53
浏览 20
已采纳

图像未使用android中的缓冲读取器存储在服务器上

First I am consverting user selected image to base 64 string then i am sending this image to php server, There i am not getting complete base64 string. I don't know whats the problem.

Here is code:-

 File file = new File(imagepath);
 //encodeImagetoString convert image to base64 string
 encodeImagetoString();
 //encodedString is a base64 string
 String imageString= encodedString; 
 EditText text= (EditText) rootView.findViewById(R.id.editText2);
 String shayaritext=text.getText().toString();
 Spinner mySpinner=(Spinner) rootView.findViewById(R.id.spinner1);
 String catValue = mySpinner.getSelectedItem().toString();
 UserFunctions userFunction = new UserFunctions();
 json = userFunction.uploadShayariData(shayariName,catValue,shayaritext, imageString);

uploadShayariData function:-

 @SuppressWarnings({ "deprecation", "deprecation" })
public JSONObject uploadShayariData(String name,String catValue,String shayaritext,String image){
    // Building Parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tag", shayariUpload));
    params.add(new BasicNameValuePair("cat", catValue));
    params.add(new BasicNameValuePair("title", name));
    params.add(new BasicNameValuePair("data", shayaritext));
    params.add(new BasicNameValuePair("image", image));

    // getting JSON Object
    JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
    // return json
    return json;
    }

PHP code:-

  $image3=$_POST['image'];
  // Get file name posted from Android App
  $filename = "a.png";
 // Decode Image
 $binary=base64_decode($base);
 header('Content-Type: bitmap; charset=utf-8');
 // Images will be saved under 'www/imgupload/uplodedimages' folder
 $file = fopen('uploads/'.$filename, 'wb');
 // Create File
 fwrite($file, $binary);
 fclose($file);
  • 写回答

1条回答 默认 最新

  • duanji1482 2014-11-29 08:01
    关注

    You can do this with a Multipart post request:(This way, you dont need to create json)

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(serverURL);
    MultipartEntity postEntity = new MultipartEntity();
    File file = new File("Your File path on SD card");
    postEntity.addPart("fileupload", new FileBody(file, "image/jpeg"));
    postEntity.addPart("loginKey", new StringBody(""+loginKey));
    postEntity.addPart("message", new StringBody(message));
    postEntity.addPart("token", new StringBody(token));
    post.setEntity(postEntity);
    response = client.execute(post);
    

    You have to add this mime4j library. http://sourceforge.net/projects/mime4j/?source=dlp

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥15 小红薯封设备能解决的来
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'
  • ¥15 vue+element项目中多tag时,切换Tab时iframe套第三方html页面需要实现不刷新
  • ¥50 深度强化学习解决能源调度问题