duankeng1911 2015-08-25 03:30
浏览 91

图片没有上传

When I try to hit the register button the data is getting uploaded in the mysql table but the image which im sending is not getting uploaded on the server.I've created a folder in server where all the images will be stored. And the path is given in the PHP file.

PHP File :

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

$decodedImage=base64_decode("$image");

file_put_contents("pictures/" . $image . ".JPG", $decodedImage);
?>

Register.java

public class Register extends Activity implements View.OnClickListener{

    EditText etName, etAge, etUsername, etPassword;
    Button bRegister,bUpload;
    ImageView imageUp;
    private static final int RESULT_IMAGE=1;
    private static final String SERVER_ADDRESS="http://swapnilsaraf.esy.es/";


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

        etName = (EditText) findViewById(R.id.etName);
        etAge = (EditText) findViewById(R.id.etAge);
        etUsername = (EditText) findViewById(R.id.etUsername);
        etPassword = (EditText) findViewById(R.id.etPassword);
        bRegister = (Button) findViewById(R.id.bRegister);

        imageUp=(ImageView)findViewById(R.id.upImage);

        bRegister.setOnClickListener(this);

        imageUp.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bRegister:
                String name = etName.getText().toString();
                String username = etUsername.getText().toString();
                String password = etPassword.getText().toString();
                int age = Integer.parseInt(etAge.getText().toString());
                Bitmap image=((BitmapDrawable)imageUp.getDrawable()).getBitmap();
                User user = new User(name, age, username, password);
                registerUser(user);

                 uploadImage ui=new uploadImage(image);
                 ui.execute();

                break;


            case R.id.upImage:
                Intent i=new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i,RESULT_IMAGE);

                Toast.makeText(getApplicationContext(),"Selecting Image",Toast.LENGTH_LONG).show();
                break;

        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==RESULT_IMAGE && resultCode==RESULT_OK && data!=null){
            Uri selectedImage=data.getData();
            imageUp.setImageURI(selectedImage);

            Toast.makeText(getApplicationContext(),"Image Selected",Toast.LENGTH_LONG).show();
        }
    }


    public class uploadImage extends AsyncTask<Void, Void,Void>{

        Bitmap image;

        public uploadImage(Bitmap image){
            this.image=image;
        }

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

            ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
            image.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
            String encodedImage= Base64.encodeToString(byteArrayOutputStream.toByteArray(),Base64.DEFAULT);

            ArrayList<NameValuePair> dataToSend=new ArrayList<NameValuePair>();

            dataToSend.add(new BasicNameValuePair("image",encodedImage));
            HttpParams httpRequestParams=getHttpRequestParams();
            HttpClient client= new DefaultHttpClient(httpRequestParams);

            HttpPost post1=new HttpPost(SERVER_ADDRESS + "upload.php");
            try{
                 post1.setEntity(new UrlEncodedFormEntity(dataToSend));
                 client.execute(post1);
            }
            catch(Exception e){
                e.printStackTrace();
            }

            return null;

        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            a
            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;
    }

    private void registerUser(User user) {
        ServerRequests serverRequest = new ServerRequests(this);
        serverRequest.storeUserDataInBackground(user, new GetUserCallback() {
            @Override
            public void done(User returnedUser) {
                Intent loginIntent = new Intent(Register.this, Login.class);
                startActivity(loginIntent);
            }
        });
    }
}
  • 写回答

3条回答 默认 最新

  • donglu9134 2015-08-25 03:52
    关注

    To upload image in PHP, use below code

    <?php
    $photoname=$_FILES['photo']['name'];
    $tmp_name=$_FILES['photo']['tmp_name'];
    $destinationpath="img/".$photoname;
    $moveimage=move_uploaded_file($tmp_name,$destinationpath);
    ?>
    

    You can't use $image= $_POST["image"] for images, its only for text.

    thanks

    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示