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

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

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题