0

有什么方法可以在不使用任何外部库的情况下将图像从 android 上传到 php 服务器?如果可以的话,给我举个例子。

4

2 回答 2

0

首先将您的图像转换为基本的 64 字节数组编码字符串。之后将其发送到 php。在服务器端提取。然后将该字符串存储在 MySQL 中。之后将该字符串发送到android客户端。提取图像字符串并使用 base 64 解码进行解码。之后,您将获得字节数组,您可以简单地在图像视图中显示。供您参考,我将展示一些代码

     String imagedata = Base64.encodeToString(thumbnailArray,Base64.DEFAULT);
            mJobject.put("imagebyte",imagedata);
              mJArray.put(mJobject);

            JSONArray json=new JSONArray(response);
            JSONObject jo = null;

           imageArray=new String[json.length()];

         imageArray[i]=jo.getString("imageid");

          completeImage= Base64.decode(imageArray[0],Base64.DEFAULT); 
Bitmap bitmap = BitmapFactory.decodeByteArray(completeImage , 0, completeImage.length);
于 2014-02-17T06:22:58.770 回答
0

看到这里是完整的代码试试这个

图像保存到服务器

  AsyncTask<Void, Void, HttpEntity> editProfileTask = new AsyncTask<Void, Void, HttpEntity>() {

  @Override
    protected HttpEntity doInBackground(Void... params) {          
     HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("Your url"); // your url

  try {                     
    MultipartEntity reqEntity = new MultipartEntity();
    reqEntity.addPart("firstname",new StringBody(firstnameEV.getText().toString(),
    "text/plain",Charset.forName("UTF-8")));
               "text/plain",Charset.forName("UTF-8")));
   if (file != null) {
       reqEntity.addPart("image",new FileBody(((File) file),"application/zip"));
      }         
      httppost.setEntity(reqEntity);
       HttpResponse resp = httpclient.execute(httppost);          
     HttpEntity resEntity = resp.getEntity();
     return resEntity;
    } catch (ClientProtocolException e) {
      e.printStackTrace();
     } catch (IOException e) {
     e.printStackTrace();
     }return null;
     }
    @Override
  protected void onPostExecute(HttpEntity resEntity) {
 if (resEntity != null) {             
   try {
     JSONObject responseJsonObject = new JSONObject(EntityUtils.toString(resEntity));
      String status = responseJsonObject.getString("status");             
       if (status.equals("success")) {
            Toast.makeText(activity, "Your Profile is updated", Toast.LENGTH_LONG).show();
             String data = responseJsonObject.getString("data");              
             isUpdatedSuccessfully=true;            
       } else {
          Toast.makeText(activity, "Profile not updated", Toast.LENGTH_LONG).show();
     }
    } catch (Exception e) {
   e.printStackTrace();
  }
    }            
  }
   };
  editProfileTask.execute(null, null, null);
于 2014-02-17T06:33:54.410 回答