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

时间:2014-11-29 07:53:27

标签: java php android json

首先我将用户选择的图像转换为64字符串然后我将此图像发送到php服务器,我没有得到完整的base64字符串。我不知道这个问题是什么。

这是代码: -

 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函数: -

 @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代码: -

  $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 个答案:

答案 0 :(得分:0)

您可以使用Multipart post请求执行此操作:(这样,您不需要创建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);

您必须添加此mime4j库。 http://sourceforge.net/projects/mime4j/?source=dlp