Android客户端从PHP服务器接收图像

时间:2018-07-12 08:33:29

标签: php android inputstream

我正在尝试通过PHP脚本发送图像,因此ImageView在Android客户端上发生了变化。

目前,我收到了这个消息,因此图像没有变化。

D/skia: --- SkAndroidCodec::NewFromStream returned null

在解决此问题方面的任何帮助将不胜感激。干杯:)

这是我的Android代码:

public class MainActivity extends AppCompatActivity {

ImageView imageView;

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

    imageView = (ImageView) findViewById(R.id.imageView);

}

private class getImage extends AsyncTask<String, String, String> {

    HttpURLConnection conn;
    URL url = null;

    public getImage(){
    }

    @Override
    protected String doInBackground(String... params) {
        Log.d("TAG", "CALLED");

        try {
            // Enter URL address where your php file resides
            url = new URL(<absolute_path_to_file>);

        } catch (MalformedURLException e) {
            e.printStackTrace();
            return e.toString();
        }
        try {
            // Setup HttpURLConnection class to send and receive data from php and mysql
            conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(15000);
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("POST");

            // setDoInput and setDoOutput to true as we send and recieve data
            conn.setDoInput(true);
            conn.setDoOutput(true);

            // add parameter to our above url
            String weekDay;
            SimpleDateFormat dayFormat = new SimpleDateFormat("EEEE", Locale.ENGLISH);

            Calendar calendar = Calendar.getInstance();
            weekDay = dayFormat.format(calendar.getTime());

            Uri.Builder builder = new Uri.Builder().appendQueryParameter("item_index", "FOUR");
            String query = builder.build().getEncodedQuery();

            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            writer.write(query);
            writer.flush();
            writer.close();
            os.close();
            conn.connect();

        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            return e1.toString();
        }

        try {

            int response_code = conn.getResponseCode();

            // Check if successful connection made
            if (response_code == HttpURLConnection.HTTP_OK) {

                // Read data sent from server
                InputStream input = conn.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                StringBuilder result = new StringBuilder();
                String line;

                while ((line = reader.readLine()) != null) {
                    result.append(line);
                }

                // Pass data to onPostExecute method
                return (result.toString());

            } else {
                return("Connection error");
            }

        } catch (IOException e) {
            e.printStackTrace();
            return e.toString();
        } finally {
            conn.disconnect();
        }
    }

    @Override
    protected void onPostExecute(String result) {

        InputStream is;

        try{
            is = conn.getInputStream();
            //get image from server
            Bitmap bitmap = BitmapFactory.decodeStream(is);
            imageView.setImageBitmap(bitmap);
            is.close();
        } catch (IOException e){
            Log.d("TAG", "ERROR: " + e);
        }

    }
}

}

这是我的PHP服务器脚本:

<?php

if(isset($_POST['item_index'])){

$filename = "leo.jpg";
$location = "leo.jpg";
$mimeType="application/octet-stream";

if(!file_exists($location))
{ header ("HTTP/1.0 404 Not Found");
  return;
}

$size=filesize($location);
$time=date('r',filemtime($location));
#html response header
header('Content-Description: File Transfer');
header("Content-Type: $mimeType");
header('Cache-Control: public, must-revalidate, max-age=0');
header('Pragma: no-cache');
header('Accept-Ranges: bytes');
header('Content-Length:'.($size));
header("Content-Disposition: inline; filename=$filename");
header("Content-Transfer-Encoding: binary\n");
header("Last-Modified: $time");
header('Connection: close');

ob_clean();
flush();
readfile($location);

}


?>

0 个答案:

没有答案