开发者社区> 问答> 正文

尝试将图像插入sqlite数据库API 21

我是android的新手,所以如果这是一个简单的问题,请原谅我,但是我试图在API 21中将图像插入到数据库中,所以我无法使用Files.readAllBytes。我在这里找到了一些将图像转换为数组的代码,但不确定该如何工作,也不确定其返回的内容。然后,我想使用数组并做一个ContentValues.put("Head", (the array) )。提前谢谢大家。

public boolean insertAvatar(Image Head) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    //File head = new File("C:capaa\\src\\main\\res\\drawable\\head2.png");

    File head = new File("C:capaa\\src\\main\\res\\drawable\\head2.png"); 
    try {
        read(head); // calling the convert method on my head file 
    }catch (IOException e){ }
    ContentValues.put("Head",head); // this is the line im not sure about 

    long ins = db.insert("Avatar", null, contentValues);
    return true;
}

//converts to bytes
public byte[] read(File file) throws IOException {
    ByteArrayOutputStream ous = null;
    InputStream ios = null;
    try {
        byte[] buffer = new byte[4096];
        ous = new ByteArrayOutputStream();
        ios = new FileInputStream(file);
        int read = 0;
        while ((read = ios.read(buffer)) != -1) {
            ous.write(buffer, 0, read);
        }
    }finally {
        try {
            if (ous != null)
                ous.close();
        } catch (IOException e) {
        }

        try {
            if (ios != null)
                ios.close();
        } catch (IOException e) {
        }
    }
    return ous.toByteArray();
}

展开
收起
Puppet 2019-12-03 17:32:22 738 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
Spring Boot2.0实战Redis分布式缓存 立即下载
CUDA MATH API 立即下载
API PLAYBOOK 立即下载