如何使用http请求发送图像

时间:2019-06-26 21:20:38

标签: android

我正在开发一个应用程序,该应用程序将发送带有2个字符串的图像,但是我在该领域的经验不足,因此我在最后一两天迷路了,因为我从脚本开始就试图弄清楚这一点,但是没有为我工作

这是我处理过的脚本部分,后面有一些PHP,一旦从画廊中选择了它,便会在我的活动中显示一个图像,并带有一些我理解但实际上并不适合我的逻辑< / p>

那是我的java代码:

package com.example.tuteur;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;

public class ajouter_photo extends AppCompatActivity implements View.OnClickListener {
    ImageView upload_img;
    EditText img_text;
    EditText img_titre;
    Button img_but;
    private static final int result_load_img = 1;
    private static final String server = "http://192.168.1.3/pfe/saveImg.php";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ajouter_photo);

        upload_img=findViewById(R.id.photo_upload);
        img_but=findViewById(R.id.upload_button);
        img_text=findViewById(R.id.upload_text_corps);
        img_titre=findViewById(R.id.upload_text_title);

       upload_img.setOnClickListener(this);
        img_but.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
    switch (v.getId())
    {
        case(R.id.photo_upload):
            Intent galleryIntent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(galleryIntent,result_load_img);
            break;
        case(R.id.upload_button):
            Bitmap image =((BitmapDrawable) upload_img.getDrawable()).getBitmap();
            new uploadimg(img_text.getText().toString(),image,img_titre.getText().toString());
            break;

    }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if( requestCode==result_load_img && resultCode==RESULT_OK && data != null){
            Uri  selectedimg = data.getData();
            upload_img.setImageURI(selectedimg);
        }
    }
     private class uploadimg extends AsyncTask<Void , Void , Void> {
        String text;
        Bitmap image;
        String titre;

         public uploadimg(String text, Bitmap image,String titre) {
             this.text = text;
             this.image = image;
             this.titre=titre;
         }
         @Override
         protected Void doInBackground(Void ... params) {
             ByteArrayOutputStream byteArrayOutputStream = new  ByteArrayOutputStream();
             image.compress(Bitmap.CompressFormat.JPEG, 100,byteArrayOutputStream);
             String encodedImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(),Base64.DEFAULT);

             ArrayList<NameValuePair> dataToSend = new ArrayList<>();
             dataToSend.add(new BasicNameValuePair("text",text));
             dataToSend.add(new BasicNameValuePair("image",encodedImage));
             dataToSend.add(new BasicNameValuePair("titre",titre));
             HttpParams httpRequest = getHttpRequestParams();
             HttpClient client = new DefaultHttpClient(httpRequest);
             HttpPost post =new HttpPost(server);
             try {
                 post.setEntity(new UrlEncodedFormEntity(dataToSend));
                 client.execute(post);
             }
             catch(Exception e)
             {
                 Toast.makeText(getApplicationContext(),"erreur" , Toast.LENGTH_SHORT).show();
                 e.printStackTrace();
             }

             return null;
         }

         @Override
         protected void onPostExecute(Void aVoid) {
             super.onPostExecute(aVoid);
             Toast.makeText(getApplicationContext(),"L'image est envoyé",Toast.LENGTH_SHORT).show();
         }


     }
     private HttpParams getHttpRequestParams()
     {
         HttpParams httpRequestParams = new BasicHttpParams();
         HttpConnectionParams.setConnectionTimeout(httpRequestParams,1000*30);
         HttpConnectionParams.setSoTimeout(httpRequestParams,1000*30);
         return  httpRequestParams;
     }
}

这是我的PHP,它只是将映像存储在服务器中,而不是在尝试使用Blob之前已经测试过的数据库

<?php 
    $name = $_POST["text"];
    $image=$_POST["image"];

    $decodedImage = base64_decode("$image");
    file_put_contents("pictures". $name . ".PNG" , $decodedImage);

 ?>

0 个答案:

没有答案