Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用位图或drawable文件夹存储图像并从sqlite数据库检索_Android_Sqlite_Bitmap_Android Sqlite - Fatal编程技术网

Android 如何使用位图或drawable文件夹存储图像并从sqlite数据库检索

Android 如何使用位图或drawable文件夹存储图像并从sqlite数据库检索,android,sqlite,bitmap,android-sqlite,Android,Sqlite,Bitmap,Android Sqlite,请帮助我在sqlite中存储和检索图像,我已经为我的应用程序使用了选项卡布局。我的主要活动是选项卡片段之一,请帮助我编写确切的代码 从ImageView获取位图: Bitmap bitmap = ((BitmapDrawable)ivImage.getDrawable()).getBitmap(); 如果您有图像的位图,请使用此位图: 保存在数据库中: public void insertImg(Bitmap img) { byte[] data = getBitmapAsBy

请帮助我在sqlite中存储和检索图像,我已经为我的应用程序使用了选项卡布局。我的主要活动是选项卡片段之一,请帮助我编写确切的代码

从ImageView获取位图:

Bitmap bitmap = ((BitmapDrawable)ivImage.getDrawable()).getBitmap();
如果您有图像的位图,请使用此位图:

保存在数据库中:

public void insertImg(Bitmap img) {
        byte[] data = getBitmapAsByteArray(img); // this is a function
        //Now save this byte array data as blob in database
    }

public static byte[] getBitmapAsByteArray(Bitmap bitmap) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.PNG, 0, outputStream);
        return outputStream.toByteArray();
    }
public Bitmap getImage() {
        //fetch byte array from your query cursor
        byte[] imgByte = cur.getBlob(0);
        return BitmapFactory.decodeByteArray(imgByte, 0, imgByte.length);
    }
ivImage.setImageBitmap(getImage());
从数据库检索:

public void insertImg(Bitmap img) {
        byte[] data = getBitmapAsByteArray(img); // this is a function
        //Now save this byte array data as blob in database
    }

public static byte[] getBitmapAsByteArray(Bitmap bitmap) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.PNG, 0, outputStream);
        return outputStream.toByteArray();
    }
public Bitmap getImage() {
        //fetch byte array from your query cursor
        byte[] imgByte = cur.getBlob(0);
        return BitmapFactory.decodeByteArray(imgByte, 0, imgByte.length);
    }
ivImage.setImageBitmap(getImage());
将检索到的位图从数据库设置为ImageView:

public void insertImg(Bitmap img) {
        byte[] data = getBitmapAsByteArray(img); // this is a function
        //Now save this byte array data as blob in database
    }

public static byte[] getBitmapAsByteArray(Bitmap bitmap) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.PNG, 0, outputStream);
        return outputStream.toByteArray();
    }
public Bitmap getImage() {
        //fetch byte array from your query cursor
        byte[] imgByte = cur.getBlob(0);
        return BitmapFactory.decodeByteArray(imgByte, 0, imgByte.length);
    }
ivImage.setImageBitmap(getImage());
但将图像保存为数据库中的字节数组并不是一种好的做法。因为将图像保存和检索为字节数组可能会带来outOfMemory问题。所以,若您有从服务器获取的图像url,那个么将该url保存为数据库中的字符串,并使用保存的同一列检索它


或者,如果您有任何可绘制图像,请将该可绘制图像名称保存在数据库中,并使用相同的名称进行检索。

检查此lnk:如何将我的图像用于位图我如何使用我的20个图像管理此位图在SQLite中保存图像是个坏主意。所以,对于你的问题,我已经回答了。这就是全部。如果使用更多图像,则将面临堆大小错误或OutOfMemoryError。所以,不要这样做。是的,但我想创建一个基于广播的应用程序。我想在当前节目运行时显示节目图像,也可以在节目列表中显示,即我将图像作为我的动态图像。。