未从Android模拟器接收到服务器的图像文件

时间:2016-05-09 09:57:37

标签: android

我正在尝试将图像从android模拟器发送到php服务器。 java文件如下所示:

 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;

        }

    }

当点击上传按钮时,我上传了图像,但是没有在php上收到图像文件。

php代码如下所示:

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

可能出了什么问题?

3 个答案:

答案 0 :(得分:1)

张你的

  

String SERVERADDRESS =&#34; http://localhost&#34 ;;

  

String SERVERADDRESS =&#34; http://10.0.2.2&#34 ;;

for emulator,

在LAN上的电话上使用机器IP时更改它

答案 1 :(得分:0)

你能检查你发送的是什么意思是它发送的正确数据有时解码和编码会产生问题

答案 2 :(得分:0)

我没有得到你的意思&#34;图像文件没有在php&#34;? 您确定您的服务器是否收到了Android客户端的请求? 无论如何,我认为我们不需要使用Base64编码来上传图像。让我们使用Multipart上传。