将图像保存到数据库

时间:2016-02-17 19:15:45

标签: android sqlite

我必须下载文件(图像)将其保存到文件夹,如果目标文件夹的大小超过50MB,则从该文件夹中删除最后修改的文件,直到目标文件夹的大小为#39 ;将再次< 50MB。

现在不是将数据保存到文件夹,而是将其放入 SQLite数据库。

通过将文件存储到SQLite数据库中,我是否将真实文件放入数据库文件(在这种情况下是URL中的图像),还是只存储有关存储文件的信息(如上次修改日期)?

这是我正在使用的简单下载方法,

public static byte[] download(Context context, String adress) {
        byte[] image = new byte[2048];
        //Downloading an image

        if (Patterns.WEB_URL.matcher(adress.toString().trim()).matches()) {

            try {
                urlad = new URL(adress.trim());
                filename = Uri.parse(adress.toString()).getLastPathSegment();
                outputFile = new File(filePathFolder, filename);
                InputStream is = urlad.openStream();

                OutputStream os = new FileOutputStream(outputFile);

                int ImageLength;

                while ((ImageLength = is.read(image)) != -1) {
                    os.write(image, 0, ImageLength);
                }



                is.close();
                os.close();

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            Toast.makeText(context.getApplicationContext(), "Please insert valid url adress", Toast.LENGTH_SHORT).show();
        }

        return image;
    }

0 个答案:

没有答案
相关问题