java - 如何使用php解码图像文件

标签 java php android

我正在使用 PHP 创建一个 Android 应用程序和一个网站。我将编码图像从 Android 应用程序存储到数据库。我想在网站上显示同一张图片。我尝试了很多,但没有在网站上显示该图像。如何使用 php 解码同一图像并在网站中显示。

这个方法,我用来对来自应用程序的图像进行编码:

private String encodeTobase64(Bitmap image) {
    Bitmap bitmap_image = image;
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap_image.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] b = stream.toByteArray();
    String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
    return imageEncoded;
}

此行在 PHP 中显示图像:

$query = mysql_query("select * from ACCOUNT where (company_email='$email')") or die(mysql_error());
$value = mysql_fetch_array($query);
$image = $value['PROFILE_PICTURE'];

echo '<img class="img-circle" src="data:image/jpeg;base64,' . base64_encode($image) . '" width="40" height="40"/>';

请大家帮帮我! 提前致谢...

最佳答案

将 Base 64 字符串存储到数据库中并不是一个好的做法。

首先,将您的 Base 64 字符串转换为图像并将图像路径存储在数据库中。

您可以借助以下函数将 Base 64 字符串解码为图像:

function convert_base64_to_image($base64, $image_path) {
    $file = fopen($image_path, "wb"); 
    $image_raw_data = explode(',', $base64);
    // get decodable part of image
    fwrite($file, base64_decode($image_raw_data[1])); 
    fclose($file); 
    return $image_path;
}

现在将图像路径存储在数据库中,您可以直接使用该路径来显示您的图像。

echo '<img class="img-circle" src="$image_path" width="40" height="40"/>';

关于java - 如何使用php解码图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40283792/

相关文章:

java - 如何从字符串中获取 Resource(int) - Android

java - JButton ImageIcon 不工作

php - 无法包含 Wordpress 主题文件夹中的 PHP 文件

php - 是否可以打开函数参数计数不匹配的错误?

android - 如何在 SQLITE Android 中添加子查询以及 'IN'?

Java 在设计上避免了instanceof

java - Java中s15.16定点数的平方根

php - 我可以 JOIN 两个表,在 MySQL 中一个表的每一行只有一个匹配项吗?

java - Android picasso : Method call should happen from the main thread

java - 如何从 MainActivity 添加到 network_security_config