Rohit S
Rohit S

Reputation: 395

Image file not received from android emulator to server

I am trying to send an image from android emulator to the php server. The java file is as shown below :

 public class MainActivity extends Activity implements OnClickListener  {

        private static final int RESULT_LOAD_IMAGE = 1;
        private static final String SERVERADDRESS = "http://localhost"; 
        // 14.99.139.53

        ImageView imageToUpload, downloadedImage;
        Button bUploadImage, bDownloadImage;
        EditText uploadImageName, downloadImageName;



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

            imageToUpload = (ImageView) findViewById(R.id.imageToUpload);
            downloadedImage = (ImageView) findViewById(R.id.downloadedImage);

            bUploadImage = (Button) findViewById(R.id.bUploadImage);
            bDownloadImage = (Button) findViewById(R.id.bdownloadImage);

            uploadImageName = (EditText) findViewById(R.id.etUploadName);
            downloadImageName = (EditText) findViewById(R.id.etDownlaodName);

            imageToUpload.setOnClickListener(this);
            bUploadImage.setOnClickListener(this);
            bDownloadImage.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {

            switch(v.getId()){

            case R.id.imageToUpload:

                Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(galleryIntent,RESULT_LOAD_IMAGE );
                break;

            case R.id.bUploadImage:
                Bitmap image = ((BitmapDrawable) imageToUpload.getDrawable()).getBitmap() ;   
                new UploadImage(image, uploadImageName.getText().toString()).execute();

                break;
            case R.id.bdownloadImage:
                break;
            }
        }

        @Override
        public void startActivityForResult(Intent intent, int requestCode) {
            // TODO Auto-generated method stub
            super.startActivityForResult(intent, requestCode);
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub

            super.onActivityResult(requestCode, resultCode, data);
            if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null){

                Uri selectedImage = data.getData();
                imageToUpload.setImageURI(selectedImage);
            }
        }

        private class UploadImage extends AsyncTask<Void, Void, Void>{

            Bitmap image;
            String name;

            public UploadImage(Bitmap image, String name){
                this.image = image;
                this.name = name;

            }

            @Override
            protected Void doInBackground(Void... arg0) {

                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                image.compress(Bitmap.CompressFormat.PNG, 100, baos);
                String encodedImage = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
                ArrayList<NameValuePair> dataToSend = new ArrayList();
                dataToSend.add(new BasicNameValuePair("image", encodedImage));
                dataToSend.add(new BasicNameValuePair("name", name));

                HttpParams httpRequestParams = getHttpRequestParams();
                HttpClient client = new DefaultHttpClient(httpRequestParams);
                HttpPost post = new HttpPost(SERVERADDRESS + "/SavePicture.php");

                try{
                    post.setEntity(new UrlEncodedFormEntity(dataToSend));
                    client.execute(post);


                }catch(Exception e)
                {
                    e.printStackTrace();
                }


                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                // TODO Auto-generated method stub
                super.onPostExecute(result);
                Toast.makeText(getApplicationContext(), "Image Uploaded" , Toast.LENGTH_SHORT).show();


            }
        }

        private HttpParams getHttpRequestParams(){

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

        }

    }

When the upload button is clicked then, I get the toast as image uploaded but the image file is not received at the php.

The php code is as shown below :

    <?php
        $name = $_POST["name"];
        $image = $_POST["image"];
$decodedImage = base64_decode("$image");
        file_put_contents("rohit/" . $name . ".JPG",$decodedImage);
    ?>

What might have gone wrong ?

Upvotes: 0

Views: 95

Answers (3)

Gaurav
Gaurav

Reputation: 3773

Chang your

String SERVERADDRESS = "http://localhost";

to

String SERVERADDRESS = "http://10.0.2.2";

for emulator,

change it with you machine IP when you are using it on phone on your LAN

Upvotes: 1

Haven
Haven

Reputation: 546

I don't get what you mean by "image file is not received at the php"? Are you sure that your server got request from Android client? Anyway, I don't think we need to use Base64 encode to upload an image. Let's use Multipart upload.

Upvotes: 0

witted_coder
witted_coder

Reputation: 161

Can you check what you are sending I mean is it the correct data sent sometimes the decode and encode gives problem

Upvotes: 0

Related Questions