【问题标题】:It is possible in android to retrieve image from MySQL database and display it in imageview?在 android 中可以从 MySQL 数据库中检索图像并将其显示在 imageview 中吗?
【发布时间】:2014-01-31 05:17:08
【问题描述】:

我将创建一个这样的活动供稿:

我想知道,我将如何检索图像以及如何将其存储在我的 MySQL 数据库中。帮帮我!

【问题讨论】:

  • 您只需要在数据库中保存图像路径并使用该路径检索图像
  • 是的,这是可能的..您可以将其保存为 byte[] 数组,也可以将图像路径或图像名称保存在数据库中并访问它..
  • 如果我使用php如何访问它?我已经在我的 cpanel 中保存了一张图片(它是一个主机/域),我为此付费了。
  • @IamAndroida:参考我的答案中给出的链接,您应该了解如何处理 php 与 android ..
  • @Angel 我正在使用图像路径本身进行检索,但我得到的是 android 模拟器中默认的虚拟图像文件,但我在编译时无法在模拟器中找到我的图像

标签: android listview imageview


【解决方案1】:
  1. 下载图片
  2. 将其保存为数据库中的字符串

这里是怎么做的

  1. 要下载图片,请使用Universal Image Loader

    ImageLoader imageLoader = ImageLoader.getInstance();
    imageLoader.init(ImageLoaderConfiguration.createDefault(context));
    OR
    ImageLoaderConfiguration config = new
        ImageLoaderConfiguration.Builder(context)
        .maxImageWidthForMemoryCache(800)
        .maxImageHeightForMemoryCache(480)
        .httpConnectTimeout(5000)
        .httpReadTimeout(20000)
        .threadPoolSize(5)
        .threadPriority(Thread.MIN_PRIORITY + 3)
        .denyCacheImageMultipleSizesInMemory()
        .build();
    imageLoader.init(config);
    options = new DisplayImageOptions.Builder()
        .showStubImage(R.drawable.loading)
        .cacheInMemory()
        .cacheOnDisc()
        .build();
    imageLoader.displayImage(ProductsImageURL,imagView,options, new ImageLoadingListener(){});
    

在此 ImageLoadingListener 中,您将获得一个在加载完成时调用的方法并将图像保存为字符串

或进行任何 http 连接以下载图像,如此处How to download and save an image in Android

  1. 下载图片后,将其转换为字符串并存储在数据库中

    public Bitmap StringToBitMap(String encodedString) {
        try {
            byte[] encodeByte = Base64.decode(encodedString, Base64.DEFAULT);
            Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0,
                    encodeByte.length);
            return bitmap;
        } catch (Exception e) {
            e.getMessage();
            return null;
        }
    }
    
    public String BitMapToString(Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] b = baos.toByteArray();
        String temp = Base64.encodeToString(b, Base64.DEFAULT);
        return temp;
    }
    

【讨论】:

    【解决方案2】:

    如果您想将图像本身存储到数据库中,以下是一般步骤。

    我。使用其 URL 检索图像并将其作为 byte[] 保存在内存中。有几种方法可以做到这一点,包括创建一个 ByteArrayBuffer 并将字节附加到其末尾。然后,您可以使用 ByteArrayBuffer.toByteArray() 检索最终的 byte[]。

    二。您需要创建一个包含 BLOB 类型列的表,例如

    CREATE TABLE image (id INTEGER PRIMARY KEY AUTOINCREMENT, url TEXT, data BLOB)
    

    三。然后,您可以像插入任何其他数据类型一样插入图像 byte[],例如通过创建 ContentValues 然后将其传递给 SQLiteDatabase.insert()。

    四。从 SQLite 中检索 byte[] 并构建图像。使用 Cursor.getBlob() 抓取 byte[] 然后执行:

    ByteArrayInputStream byteArrayImageStream = new ByteArrayImageStream(yourByteArray);
    Bitmap yourImage = BitmapFactory.decodeStream(byteArrayImageStream);
    

    【讨论】:

    • 这适用于使用托管数据库吗?我没有使用 SQLitedtabase,我使用的是 mysql 数据库,我还使用 php 作为 android 和 mysql 之间的桥梁
    • 这是一个完全不同的场景,但它似乎完全可行。这是我在搜索“php 将图像下载到 mysql”时发现的第一个链接:phpriot.com/articles/images-in-mysql 将图像保存到 MySQL 后,您只需将 Android 应用程序调用到 PHP 中并获取图像即可。
    猜你喜欢
    • 2023-03-09
    • 2012-05-02
    • 2021-09-29
    • 1970-01-01
    • 2020-05-09
    • 2014-06-15
    • 1970-01-01
    • 1970-01-01
    • 2016-02-07
    相关资源
    最近更新 更多