Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php在android中向服务器上传图像_Php_Android_Server - Fatal编程技术网

Php在android中向服务器上传图像

Php在android中向服务器上传图像,php,android,server,Php,Android,Server,Im使用php脚本,允许从gallery中选择图像并将其上载到在线web托管服务。图像已上载,但上载的.jpg文件为空,名称为0.jpg,大小也显示为0字节。当我试图打开它显示的图像时 无法显示图像 因为它包含错误 可能的原因是什么? php代码如下所示 <?php $name = $_POST['name']; //image name $image = $_POST['image']; //image in string format //decode th

Im使用php脚本,允许从gallery中选择图像并将其上载到在线web托管服务。图像已上载,但上载的.jpg文件为空,名称为0.jpg,大小也显示为0字节。当我试图打开它显示的图像时

无法显示图像 因为它包含错误

可能的原因是什么? php代码如下所示

 <?php
    $name = $_POST['name']; //image name
    $image = $_POST['image']; //image in string format

    //decode the image
    $decodedImage = base64_decode($image);

    //upload the image
    file_put_contents("pic/".$name.".jpg", $decodedImage);
android代码是:

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;


public class MainActivity extends Activity {
    //define global views variable
    public ImageView imageView;
    public Button   selectImage,
                    uploadImage;
    public String SERVER = "http://testimage.site88.net/saveImage.php",
                    timestamp;

    private static final String TAG = MainActivity.class.getSimpleName();

    private static final int RESULT_SELECT_IMAGE = 1;

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

        //instantiate view
        imageView = (ImageView) findViewById(R.id.imageView);
        selectImage = (Button) findViewById(R.id.selectImage);
        uploadImage = (Button) findViewById(R.id.uploadImage);

        //when selectImage button is pressed
        selectImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //call the function to select image from album
                selectImage();
            }
        });

        //when uploadImage button is pressed
        uploadImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //get image in bitmap format
                Bitmap image = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
                //execute the async task and upload the image to server
                new Upload(image,"IMG_"+timestamp).execute();
            }
        });

    }

    //function to select a image
    private void selectImage(){
        //open album to select image
        Intent gallaryIntent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(gallaryIntent, RESULT_SELECT_IMAGE);
    }

    /*
    * This function is called when we pick some image from the album
    * */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_SELECT_IMAGE && resultCode == RESULT_OK && data != null){
            //set the selected image to image variable
            Uri image = data.getData();
            imageView.setImageURI(image);

            //get the current timeStamp and strore that in the time Variable
            Long tsLong = System.currentTimeMillis() / 1000;
            timestamp = tsLong.toString();

            Toast.makeText(getApplicationContext(),timestamp,Toast.LENGTH_SHORT).show();
        }
    }

    private String hashMapToUrl(HashMap<String, String> params) throws UnsupportedEncodingException {
        StringBuilder result = new StringBuilder();
        boolean first = true;
        for(Map.Entry<String, String> entry : params.entrySet()){
            if (first)
                first = false;
            else
                result.append("&");

            result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
            result.append("=");
            result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
        }

        return result.toString();
    }


    //async task to upload image
    private class Upload extends AsyncTask<Void,Void,String>{
        private Bitmap image;
        private String name;

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

        @Override
        protected String doInBackground(Void... params) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            //compress the image to jpg format
            image.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
            /*
            * encode image to base64 so that it can be picked by saveImage.php file
            * */
            String encodeImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(),Base64.DEFAULT);

            //generate hashMap to store encodedImage and the name
            HashMap<String,String> detail = new HashMap<>();
            detail.put("name", name);
            detail.put("image", encodeImage);

            try{
                //convert this HashMap to encodedUrl to send to php file
                String dataToSend = hashMapToUrl(detail);
                //make a Http request and send data to saveImage.php file
                String response = Request.post(SERVER,dataToSend);

                //return the response
                return response;

            }catch (Exception e){
                e.printStackTrace();
                Log.e(TAG,"ERROR  "+e);
                return null;
            }
        }



        @Override
        protected void onPostExecute(String s) {
            //show image uploaded
            Toast.makeText(getApplicationContext(),"Image Uploaded",Toast.LENGTH_SHORT).show();
        }
    }
}

提前谢谢。

我想您只需要修复php文件。我在你的问题中编辑了它,因为它没有意义。利用现在的资源

如果你需要更多的帮助,请给我们的输出

error_log(print_r($_POST, 1));

从php日志中。

这是您的真实代码吗?91英镑;在里面???也许先解决这个问题,然后再试一次,在您尝试的同时查看php日志,并在那里发布您看到的错误,以防它不起作用。此外,您可能希望显示用于发布图像的android代码,因为它看起来并不简单,因为您希望将其作为base64编码字符串发送。我是php新手,我一直在看教程,发现了这个脚本。它有这个91;;在里面。这意味着什么。我认为它采用已上传图像的名称,图像将被转换为字符串,然后转换为Base_64。如果是,应该用什么来代替91;;??我认为时间戳在onClick中不会有值。我们仍然不知道什么不起作用。代码乍一看是正常的。我修复了你的php。给出错误消息、详细信息……什么是固定php?请参见问题中的“^”,如果它现在可以工作,请阅读以下内容: