从android到php的图像获取位置,有时错误无法获取位置

时间:2016-11-22 06:16:05

标签: php android image gps

我正在使用AS 第一种情况:我正在运行Android应用程序从PC A到设备kitkat和marshmallow,然后我正在尝试将图像上传到服务器并使用PHP获取位置。结果是当使用设备kitkat GPS时我可以得到它,但没有棉花糖

第二种情况:我正在从PC B运行Android应用程序,到设备kitkat和marshmallow,然后我正在尝试将图像上传到服务器并使用PHP获取位置。结果是我使用kitkat和marshmallow两者都无法从图像中获取GPS

第三种情况:我正在从PC B运行Android应用程序到设备kitkat和marshmallow,然后我正在尝试将图像上传到服务器并使用PHP获取位置。但是现在我改变IP以在PC A上使用IP连接服务器。结果是当我使用设备kitkat GPS我可以得到它,但使用Marshmallow仍然无法获得位置

代码中的

我已经使用了android运行时权限。没有logcat错误 我该如何解决这个问题?

和这段代码PHP

<?php
error_reporting(0);
$latitude = 0;
$longitude = 0;

$lokasi = "uploads/";
$file_name      = $_FILES['image']['name'];
$file_ext       = explode('.',$file_name);
$file_ext       = strtolower(end($file_ext));
$file_size      = $_FILES['image']['size'];
$file_tmp       = $_FILES['image']['tmp_name'];

$filename       = basename($_FILES['image']['name']);
$target_path    = $lokasi . basename($_FILES['image']['name']);
echo basename($_FILES['image']['name'])."<BR>";
//get latitude and longitude
$image_file = $file_tmp;
echo "string : ".$image_file;
if(file_exists($image_file)){
    $details = exif_read_data($image_file); 
    $sections = explode(',',$details['SectionsFound']); 

    if(in_array('GPS',array_flip($sections))){ 

        echo $latitude = number_format(format_gps_data($details['GPSLatitude'],$details['GPSLatitudeRef']),10, ',', ' '); 
        $longitude = number_format(format_gps_data($details['GPSLongitude'],$details['GPSLongitudeRef']),10, ',', ' '); 
    }
    else{
        die('GPS data not found'); 
    }
}
else{
    die('File does not exists'); 
}

function format_gps_data($gpsdata,$lat_lon_ref){
    $gps_info = array();
    foreach($gpsdata as $gps){
        list($j , $k) = explode('/', $gps);
        array_push($gps_info,$j/$k);
    }
    $coordination = $gps_info[0] + ($gps_info[1]/60.00) + ($gps_info[2]/3600.00);
    return (($lat_lon_ref == "S" || $lat_lon_ref == "W" ) ? '-'.$coordination : $coordination).' '.$lat_lon_ref;
}
///////////////////////////////

$nama_file = $latitude . "_" . $longitude . "." . $file_ext;
$target_path=$lokasi . $nama_file;


try {
    //throw exception if can't move the file
    if (!move_uploaded_file($file_tmp, $target_path)) {
        throw new Exception('Could not move file');
    }

    echo "Success";
} catch (Exception $e) {
    die('File did not upload: ' . $e->getMessage());
}
?>

和上传图片的代码

    private class UploadFileToServer extends AsyncTask<Void, Integer, String> {
        @Override
        protected void onPreExecute() {
            // setting progress bar to zero
            progressBar.setProgress(0);
            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(Integer... progress) {
            // Making progress bar visible
            progressBar.setVisibility(View.VISIBLE);

            // updating progress bar value
            progressBar.setProgress(progress[0]);

            // updating percentage value
            txtPercentage.setText(String.valueOf(progress[0]) + "%");
        }

        @Override
        protected String doInBackground(Void... params) {
            return uploadFile();
        }

        @SuppressWarnings("deprecation")
        private String uploadFile() {
            String responseString = null;

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(Config.FILE_UPLOAD_URL);

            try {
                AndroidMultiPartEntity entity = new AndroidMultiPartEntity(
                        new ProgressListener() {

                            @Override
                            public void transferred(long num) {
                                publishProgress((int) ((num / (float) totalSize) * 100));
                            }
                        });

                File sourceFile = new File(filePath);

                // Adding file data to http body
                entity.addPart("image", new FileBody(sourceFile));
                image = new FileBody(sourceFile);
                // Extra parameters if you want to pass to server
                entity.addPart("website",
                        new StringBody("www.androidhive.info"));
                entity.addPart("email", new StringBody("abc@gmail.com"));

                totalSize = entity.getContentLength();
                httppost.setEntity(entity);

                // Making server call
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity r_entity = response.getEntity();

                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == 200) {
                    // Server response
                    responseString = EntityUtils.toString(r_entity);
                } else {
                    responseString = "Error occurred! Http Status Code: "
                            + statusCode;
                }

            } catch (ClientProtocolException e) {
                responseString = e.toString();
            } catch (IOException e) {
                responseString = e.toString();
            }

            return responseString;

        }

        @Override
        protected void onPostExecute(String result) {
            Log.e(TAG, "Response from server: " + result);

            // showing the server response in an alert dialog
            showAlert(result);

            super.onPostExecute(result);
        }

    }

0 个答案:

没有答案