0

I am working on an application that would send an image with 2 strings but I am not as experienced in this field so i was lost for the last day or two trying to figure this out as i started with a script but didn't work for me

here is the part of script that i worked on which has a bit PHP behind and just show an image in my activity once it's chosen form the gallery with a bit of logic that i understood but didn't really work for me

that's my java code :

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;
     }
}

and this is my PHP which just store my image in the server not a data base it was test before I would try with blob

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

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

 ?>
Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30

0 Answers0