dongqian0763 2016-03-19 15:57
浏览 82
已采纳

图像是在android中使用php上传大小为0字节的

I m making an android app to upload the image in mysql database using php. Im saving in directory [$path = "uploads/$id.png"; $actualpath = "http://www.bsservicess.com/photoUpload/$path"].

The problem is that image is uploading to the directory but it showing the size of image as 0 byte. I dont know what's the problem.It working perfectly earlier but i dont know why it uploading the size of image of 0 byte now. I try everything but nothing works. If somebody knows the solution please help me.

 <?php 
 if($_SERVER['REQUEST_METHOD']=='POST'){

    $photo= $_POST['photo'];
    $bookname=$_POST['bookname'];
            $phoneNumber=$_POST['phoneNumber'];
            $price=$_POST['price'];

    require_once('loginConnect.php');

    $sql ="SELECT id FROM images ORDER BY id ASC";

    $res = mysqli_query($con,$sql);

    $id = 0;

    while($row = mysqli_fetch_array($res)){
            $id = $row['id'];
    }

    $path = "uploads/$id.png";

    $actualpath = "http://www.bsservicess.com/photoUpload/$path";

    $sql = "INSERT INTO images (photo,bookname,phoneNumber,price) VALUES
     ('$actualpath','$bookname','$phoneNumber','$price')";

    if(mysqli_query($con,$sql)){
        file_put_contents($path,base64_decode($photo));
        echo "Successfully Uploaded";
    }

    mysqli_close($con);
  }else{
    echo "Error";
 }
?


 enter code here> Blockquote

   package com.manali.vivek.userregistration;

   import android.app.ProgressDialog;
   import android.content.Intent;
   import android.graphics.Bitmap;``
   import android.net.Uri;
   import android.os.AsyncTask;
   import android.os.Bundle;
   import android.provider.MediaStore;
   import android.support.v7.app.ActionBarActivity;
   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 java.io.ByteArrayOutputStream;
   import java.io.IOException;
   import java.util.HashMap;


 public class image extends ActionBarActivity implements
  View.OnClickListener    
 {

   public static final String UPLOAD_URL =
    "http://www.bsservicess.com/photoUpload/uploadImage.php";
   public static final String UPLOAD_KEY = "image";

   private int PICK_IMAGE_REQUEST = 1;

   private Button buttonChoose;
   private Button buttonUpload;
   private Button buttonView;

   private ImageView imageView;

   private Bitmap bitmap;

   private Uri filePath;
   EditText et1,et2,et3;

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image);

    buttonChoose = (Button) findViewById(R.id.buttonChoose);
    buttonUpload = (Button) findViewById(R.id.buttonUpload);
    buttonView = (Button) findViewById(R.id.buttonViewImage);
    et1=(EditText)findViewById(R.id.et1);
    et2=(EditText)findViewById(R.id.et2);
    et3=(EditText)findViewById(R.id.et3);
    imageView = (ImageView) findViewById(R.id.imageView);

    buttonChoose.setOnClickListener(this);
    buttonUpload.setOnClickListener(this);
    buttonView.setOnClickListener(this);
  }

  private void showFileChooser() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), 
 PICK_IMAGE_REQUEST);
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent 
  data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data
   != null && data.getData() != null) {

        filePath = data.getData();
        try {

            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),   
       filePath);
            imageView.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
   }

   public String getStringImage(Bitmap bmp){
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] imageBytes = baos.toByteArray();


   String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    return encodedImage;
   }


   private void uploadImage(){


        class UploadImage extends AsyncTask<Bitmap,Void,String>{

        ProgressDialog loading;
        RequestHandler rh = new RequestHandler();

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(image.this, "Uploading Image",
         "Please wait...",true,true);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();

         Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
        }

        @Override
        protected String doInBackground(Bitmap... params) {

            Bitmap bitmap = params[0];
            String uploadImage = getStringImage(bitmap);
            String bookname=et1.getText().toString();
            String  phoneNumber=et2.getText().toString();
            String price=et3.getText().toString();

            HashMap<String,String> data = new HashMap<>();
            data.put(UPLOAD_KEY, uploadImage);

            data.put(Fetch.BOOK_NAME,bookname);
            data.put(Fetch.PHONE_NUMBER,phoneNumber);
            data.put(Fetch.PRICE,price);



            String result = rh.sendPostRequest(UPLOAD_URL,data);

            return result;
        }

    }

    UploadImage ui = new UploadImage();

    ui.execute(bitmap);


    }







 @Override
  public void onClick(View v) {
    if (v == buttonChoose) {
        showFileChooser();
    }
    if(v == buttonUpload){
    uploadImage();

    }


    if(v == buttonView){
        viewImage();
    }
  }


private void viewImage() {
    startActivity(new Intent(image.this, Main.class));
     }
 }

Blockquote

  • 写回答

1条回答 默认 最新

  • duanbi3151 2016-03-19 18:21
    关注

    Can you double check your code? Your $photo is never used and the $imagethat you're trying to decode is null as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 idea自动补全键位冲突
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 定制ai直播实时换脸软件
  • ¥100 栈回溯相关,模块加载后KiExceptionDispatch无法正常回溯了
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错
  • ¥15 java python或者任何一种编程语言复刻一个网页