我使用以下代码从相机捕获图像并存储到设备中。然后我通过使用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);
?>
答案 0 :(得分:0)
使用FileProvider
String serverResponseCode = uploadFiles(path.next());
public String uploadFiles(String sourceFileUri)
{
File imagePath = new File(getFilesDir(), "external_files");
imagePath.mkdir();
File imageFile = new File(imagePath.getPath(), "test.jpg");
// Write data in your file
Uri fileUri= FileProvider.getUriForFile(this, getPackageName(), imageFile);
String responseString = null;
try
{
File file = new File(fileUri.getPath());
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http:/upload.php");
AndroidMultiPartEntity entity = new AndroidMultiPartEntity();
File sourceFile = new File(fileUri.getPath());
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;
}
有关详细信息Read this