它可以在android系统从MySQL数据库中检索图像和ImageView的显示呢? [英] It is possible in android to retrieve image from MySQL database and display it in imageview?

查看:123
本文介绍了它可以在android系统从MySQL数据库中检索图像和ImageView的显示呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建活动饲料是这样的:

和我不知道,我怎么会检索图像以及如何将其存储在我的MySQL数据库。帮帮我!


解决方案

  1. 下载图片

  2. 将它保存在数据库中的字符串

下面是如何做到这一点。


  1. 要下载图像使用通用图像装载机

      ImageLoader的ImageLoader的= ImageLoader.getInstance();
    imageLoader.init(ImageLoaderConfiguration.createDefault(上下文));
    要么
    ImageLoaderConfiguration配置=新
        ImageLoaderConfiguration.Builder(上下文)
        .maxImageWidthForMemoryCache(800)
        .maxImageHeightForMemoryCache(480)
        .httpConnectTimeout(5000)
        的.htt preadTimeout(20000)
        .threadPoolSize(5)
        .threadPriority(从Thread.MIN_PRIORITY + 3)的
        .denyCacheImageMultipleSizesInMemory()
        。建立();
    imageLoader.init(配置);
    选项​​=新DisplayImageOptions.Builder()
        .showStubImage(R.drawable.loading)
        .cacheInMemory()
        .cacheOnDisc()
        。建立();
    imageLoader.displayImage(ProductsImageURL,imagView,期权,新ImageLoadingListener(){});


在此ImageLoadingListener你得到一个当加载完成,将图像保存为字符串时调用的方法。

或作出任何HTTP连接下载图片如下如何下载并保存在Android中的图片


  1. 在下载图像转换成字符串,并将其存储在数据库中。

     公共位图StringToBitMap(字符串连接codedString){
        尝试{
            字节[] EN codeByte = Base64.de code(EN codedString,Base64.DEFAULT);
            位图位图= BitmapFactory.de codeByteArray的(EN codeByte,0,
                    EN codeByte.length);
            返回位图;
        }赶上(例外五){
            e.getMessage();
            返回null;
        }
    }公共字符串BitMapToString(位图位图){
        ByteArrayOutputStream BAOS =新ByteArrayOutputStream();
        bitmap.com preSS(Bitmap.Com pressFormat.PNG,100,BAOS);
        字节[] B = baos.toByteArray();
        字符串临时= Base64.en codeToString(B,Base64.DEFAULT);
        返回温度;
    }


I am going to create an activity feed like this:

and I wonder, how will I retrieve the image and how to store it in my MySQL database. Help me out!

解决方案

  1. Download the Image
  2. Save it as string in database

Here is how to do it

  1. To download the image use the 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(){});
    

In this ImageLoadingListener you get a method that is called when loading is done and save the image as string

or make any http connection to download the image as here How to download and save an image in Android

  1. Once the image is downloaded convert it into string and store it in database

    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;
    }
    

这篇关于它可以在android系统从MySQL数据库中检索图像和ImageView的显示呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆