我正在尝试将图像上传到服务器,但 base64_decode
始终返回 null
我读了很多不同的案例,但没有任何作用。
这是我发出 http 请求之前的 java 代码:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Must compress the Image to reduce image size to make upload easy
bitmap.compress(Bitmap.CompressFormat.PNG, 50, stream);
byte[] byte_arr = stream.toByteArray();
// Encode Image to String
encodedString = Base64.encodeToString(byte_arr,Base64.NO_WRAP);
params.add(new BasicNameValuePair(TAG_IMAGE, encodedString));
...and the http request
PHP 代码:
$base=$_REQUEST['image'];
//$response["base"] = $base;
$binary=base64_decode($base,TRUE);
$response["binary"] = $binary;
header('Content-Type: bitmap; charset=utf-8');
$imagepath='the path of the image';
$file = fopen($imagepath, 'w');
fwrite($file, $binary);
fclose($file);
我也尝试过 $file = fopen($imagepath, 'wb');
仍然 $binary
总是返回 null
;
有人可以帮我理解问题是什么吗?
我还检查了 $base
,它看起来不错,我也尝试删除 \n
或 \\
但没有任何帮助
最佳答案
所以我明白了: 1.您需要保存图像的路径必须相对于服务器中的Web服务(在我的例子中是php代码),这也意味着您必须将图像保存在同一服务器上。 2. file_put_contents 更容易使用,并且可以一起替换 fopen、fwrite 和 fclose,省去麻烦。 希望对你们有帮助
关于java - 尝试上传图像时,base64_decode 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30424284/