【发布时间】:2018-06-03 19:33:59
【问题描述】:
我已使用以下代码从相机捕获图像并存储到设备中。然后我通过使用 multipartentity 按下按钮上传了这些文件。这些代码在所有版本中都可以正常工作,但在 Nougat 7.0 + 更高版本上工作失败,
String serverResponseCode = uploadFiles(path.next());
public String uploadFiles(String sourceFileUri)
{
String responseString = null;
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http:/upload.php");
AndroidMultiPartEntity entity = new AndroidMultiPartEntity();
File sourceFile = new File(sourceFileUri);
entity.addPart("image", new FileBody(sourceFile));
entity.addPart("username",new StringBody(Username));
httppost.setEntity(entity);
// Making server call
HttpResponse response = httpclient.execute(httppost);
HttpEntity r_entity = response.getEntity();
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200)
{
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;
}
我在服务器端使用了以下 PHP 代码。这些代码创建了文件夹,但图像不可用。
<?php
// Path to move uploaded files
//$target_path = "uploads/";
$year = date("Y-m-d");
$username = isset($_POST['username']) ? $_POST['username'] : '';
$target_path=$year."/".$username."/";
//If the directory doesn't already exists.
if(!is_dir($target_path))
{
//Create our directory.
mkdir($target_path,0777,true);
}
// array for final json respone
$response = array();
// getting server ip address
$server_ip = gethostbyname(gethostname());
// final file url that is being uploaded
$file_upload_url = 'http://' . $server_ip . '/' . 'Merchandise' . '/' . $target_path;
if (isset($_FILES['image']['name']))
{
$target_path = $target_path . basename($_FILES['image']['name']);
// reading other post parameters
//$email = isset($_POST['email']) ? $_POST['email'] : '';
//$website = isset($_POST['website']) ? $_POST['website'] : '';
$response['file_name'] = basename($_FILES['image']['name']);
//$response['email'] = $email;
//$response['website'] = $website;
try {
// Throws exception incase file is not being moved
if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path))
{
// make error flag true
$response['error'] = true;
$response['message'] = 'Could not move the file!';
}
// File successfully uploaded
$response['message'] = 'File uploaded successfully!';
$response['error'] = false;
$response['file_path'] = $file_upload_url . basename($_FILES['image']['name']);
}
catch (Exception $e)
{
// Exception occurred. Make error flag true
$response['error'] = true;
$response['message'] = $e->getMessage();
}
} else {
// File parameter is missing
$response['error'] = true;
$response['message'] = 'Not received any file!F';
}
// Echo final json response to client
echo json_encode($response);
?>
【问题讨论】:
-
你能分享你的错误日志吗
-
我没有收到任何错误。我已经在服务器中使用 php 处理了该请求。使用 PHP,我创建了一个文件夹并存储了文件。代码创建了文件夹,但图像没有出现在其中。
-
你检查过在邮递员中工作的 API 吗?
-
确保您上传的文件的扩展名在服务器中是允许的,并且上传的文件大小不超过服务器文件大小限制(某些服务器有 2mb)
-
是的,它正在工作。我已经上传了图片和一些数据。这些数据被插入到表中,但只是没有收到图像。仅使用 android 7.0 及更高版本上传时服务器未收到图像。
标签: android android-studio image-uploading android-7.0-nougat android-7.1-nougat