微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 如何在sqlite数据库中插入图像

我想使用这个数据库代码将图像从相机放到数据库中.保存按钮和预览按钮的代码应该是什么?

public class databases extends sqliteOpenHelper {
        public static final String data = "student.db";
        public static final String TABLE_NAME = "Student_TABLE";
        public static final String COL_1 = "Fname";
        public static final String COL_2 = "password";
        public static final String COL_3 = "Fathers_name";
        public static final String COL_4 = "Roll";
        public static final String COL_5 = "Image";
        public databases(Context context) {
            super(context, data, null, 1);
        }

        @Override
        public void onCreate(sqliteDatabase db) {
            db.execsql("create table " + TABLE_NAME + " (Fname TEXT,password TEXT,Fathers_name TEXT,Roll TEXT PRIMARY KEY,Image BLOB)");
        }

在这是包含相应按钮的活动代码.

public class home_page extends AppCompatActivity {
Button lout,homepage,showperson,picture;static final int REQUEST_IMAGE_CAPTURE = 1;
TextView showit;
String userid,pwrd,r2,r3,r4;
databases dbss;
    public void getterclass(String m)
    {
        userid=m;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page);
        lout = (Button) findViewById(R.id.out);
        homepage = (Button) findViewById(R.id.home);
        showit=(TextView) findViewById(R.id.showtextdata);
        showperson=(Button)findViewById(R.id.showdata);
        picture=(Button)findViewById(R.id.images);
        dbss=new databases(home_page.this);
        getSupportActionBar().hide();
        homepage.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent redirect=new Intent(home_page.this,home_page.class);
                startActivity(redirect);
            }
        });

        lout.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent logout=new Intent(home_page.this,log_in.class);
                        startActivity(logout);
            }
        });
        showperson.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Cursor sh=dbss.givedata(userid);
                StringBuffer buf=new StringBuffer();
                buf.append("First name :" + sh.getString(0) + "\n");
                buf.append("password :" + sh.getString(1) + "\n");
                buf.append("Father's name :" + sh.getString(2) + "\n");
                buf.append("Roll :" + sh.getString(3) + "\n\n");
                showit.setText(buf.toString());
            }
        });
        picture.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                    startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
                }
                databases help=new databases(home_page.this);
            }
        });

getterclass()只不过是将用户的个人信息检索到其他活动中,但是当我尝试在同一个活动中显示数据时,这种信息也不起作用.

解决方法:

您将图像解压缩为byte [],然后将其转换为字节,作为十六进制字符串,然后将该字符串括在X’your_hexadecimal_string’中

>例如它可能是X’FFFEFDFCFBFA9 ……..’

并使用该字符串作为通过本机sql插入的值

>例如INSERT INTO your_table(image_column)VALUES(X’FFFEFDFCFBFA9 ……..’)

或者您可以使用sqliteDatbase插入方便方法,该方法将Contentvalues对象作为第3个参数.你可以设置Contentvalues对象

ContentValues cv = new Contentvalues();
cv.put("Image",your_image_as_a_byte_array);
db.insert("the_table_name",null,cv);

但是,存储图像可能很麻烦,不建议存储图像,特别是如果它们平均大小超过100k.

如果图像接近或大于2M,您将无法使用标准Android sqlite API检索该图像,因为CursorWindow具有2M的限制.即使在1M,你也许可以在CursorWindow中获得1个图像.

建议的方法是将路径或路径的一部分存储到图像,并在需要时通过其路径检索图像.

说(上面),以下是一个应用程序,如果它低于100K(公共静态最终int MAX_FILE_SIZE = 100 * 1024;)将存储在数据库中,或者存储路径并通过路径检索图像或根据存储的数据库.

该应用程序有两个ListViews与图像描述(最右边包括图像的路径).单击其中一个项目可显示图像.

例如

enter image description here

有超过100k的图像,one funny.png低于100k: –

enter image description here

对于此示例,图像已放入Res文件夹的Raw文件夹中,然后将它们复制到data / data / files / images文件夹(其中表示应用程序包的名称): –

enter image description here

DatabaseHelper,DBHelper.java是: –

public class DBHelper extends sqliteOpenHelper {
    public static final String dbnAME = "images.db";
    public static final int DBVERSION = 1;

    // The maximum size of an image that should be stored 100K
    public static final int MAX_FILE_SIZE = 100 * 1024;

    public static final String TB_IMAGE = "image";
    public static final String COL_IMAGE_ID = BaseColumns._ID;
    public static final String COL_IMAGE_PATH = "image_path";
    public static final String COL_IMAGE_NAME = "image_name";
    public static final String COl_IMAGE_DESCRIPTION = "image_description";
    public static final String COL_IMAGE_SIZE = "image_size";
    public static final String COL_IMAGE_IMAGE = "image";

    sqliteDatabase mDB;

    /**
     * Construct DBHelper, note that it will open the database and
     * thus create it if it doesn't exist
     * @param context   a context from the invoking activity
     */
    public DBHelper(Context context) {
        super(context, dbnAME, null, DBVERSION);
        mDB = this.getWritableDatabase();
    }

    /**
     * Create the table(s)
     * @param db
     */
    @Override
    public void onCreate(sqliteDatabase db) {
        String crtsql = "CREATE TABLE IF NOT EXISTS " + TB_IMAGE +
                "(" +
                COL_IMAGE_ID + " INTEGER PRIMARY KEY, " +
                COL_IMAGE_PATH + " TEXT UNIQUE, " +
                COL_IMAGE_NAME + " TEXT, " +
                COl_IMAGE_DESCRIPTION + " TEXT, " +
                COL_IMAGE_SIZE + " INTEGER, " +
                COL_IMAGE_IMAGE + " BLOB DEFAULT x'00'" +
                ")";
        db.execsql(crtsql);
    }

    @Override
    public void onUpgrade(sqliteDatabase sqliteDatabase, int i, int i1) {

    }

    /**
     * Return a Cursor with all the rows from the image table
     * @return  The Cursor
     */
    public Cursor getimageList() {
        return mDB.query(TB_IMAGE,null,null,null,null,null,null);
    }


    /**
     * Store an image row in the image table, noting that is the image
     *  size is small than the max size that the image will be stored as a blob
     *  otherwise a blob of 1 byte is stored due to the default value.
     * @param path          the path to the image
     * @param description   a description for the image
     * @return              the id (rowid) of the row
     */
    public long addImageFromPath(String path, String description) {

        ContentValues cv = new ContentValues();
        File f = new File(path);
        InputStream is;

        // If the file doesn't exist don't store a row
        if (!f.exists()) {
            return -1;
        }

        // Always store the name, description, path and size
        cv.put(COL_IMAGE_NAME,f.getName());
        cv.put(COl_IMAGE_DESCRIPTION,description);
        cv.put(COL_IMAGE_SIZE,f.length());
        cv.put(COL_IMAGE_PATH,f.getAbsolutePath());

        // If the size is less than the max then get the filestream
        // and convert to a byte[].
        // Note if larger then the max file size the default x'00' blob
        // will be applied
        if (f.length() < MAX_FILE_SIZE) {
            byte[] buffer = new byte[(int) f.length()];
            try {
                is = new FileInputStream(f);
                is.read(buffer);
            } catch (IOException e) {
                e.printstacktrace();
                return -1;
            }
            cv.put(COL_IMAGE_IMAGE,buffer);
        }
        // Do the insert
        return mDB.insert(TB_IMAGE,null,cv);
    }

    /**
     * get the image as a bitmap from the DB if stored, otherwise get it from
     * the file, according to the id.
     * @param id    the id of the row in the image table
     * @return      the bitmap to be returned (note may be empty bitmap)
     */
    public Bitmap getimage(long id) {
        byte[] ba = new byte[0];

        // If the image is stored in the DB then extract and return the bitmap
        if (isstoredAsImage(id)) {
            return getimageAsBitMap(id);
        }
        // If not then get the respective row from the DB
        Cursor csr = mDB.query(
                TB_IMAGE,
                null,
                COL_IMAGE_ID+"=?",
                new String[]{String.valueOf(id)},
                null,
                null,
                null
        );

        // Prepare to convert the path to a file
        String path = ""; //<<<< default to  empty path
        File f = new File(path); //<<< default to empty file
        // If a valid row was found get the path and File from the row
        if (csr.movetoFirst()) {
            path = csr.getString(csr.getColumnIndex(COL_IMAGE_PATH));
            f = new File(path);
        }
        // done with the cursor so close it
        csr.close();

        // If the file exists then return the Bitmap
        if (f.exists()) {
            return BitmapFactory.decodeFile(f.getAbsolutePath());
        }
        // return an empty bitmap
        return BitmapFactory.decodeByteArray(ba,0,ba.length);
    }

    /**
     * Check to see if an image is stored in the DB,
     *  note assumes anything less than 8 bytes isn't an image
     * @param id    the id of the row in the image table
     * @return      true if like an image is stored, otherwise false
     */
    private boolean isstoredAsImage(long id) {
        boolean rv = true;
        byte[] ba = new byte[0];

        // Get the respective row from the image table
        Cursor csr = mDB.query(
                TB_IMAGE,
                null,
                COL_IMAGE_ID+"=?",
                new String[]{String.valueOf(id)},
                null,
                null,
                null
        );

        // If a row was found get the blob into byte array ba
        // if not then ready to return false
        if (csr.movetoFirst()) {
            ba = csr.getBlob(csr.getColumnIndex(COL_IMAGE_IMAGE));
        } else {
            rv = false;
        }
        // If the byte array ba is less then 8 bytes then ready to return false
        if (ba == null || ba.length < 8) {
            rv =  false;
        }
        // done with the Cursor so close it
        csr.close();
        // return the result
        return rv;
    }

    /**
     * get the image (assumes isstoredAsImage is used prior to invocation)
     * @param id    the id of the respective row
     * @return      the bitmap (may be 0 length)
     */
    private Bitmap getimageAsBitMap(long id) {
        byte[] ba = new byte[0];
        Bitmap bmp;
        Cursor csr =mDB.query(
                TB_IMAGE,
                null,
                COL_IMAGE_ID+"=?",
                new String[]{String.valueOf(id)},
                null,
                null,
                null
        );
        if (csr.movetoFirst()) {
            ba = csr.getBlob(csr.getColumnIndex(COL_IMAGE_IMAGE));
        }
        csr.close();
        return BitmapFactory.decodeByteArray(ba,0,ba.length);
    }
} 

Invoking活动MainActivity.java是: –

public class MainActivity extends AppCompatActivity {

    public static final String IMAGES_DIRECTORY = "images";
    private static File images_file;

    ArrayAdapter<String> mAdapter;
    ListView mListView01, mListView02;
    ArrayList<String> mImages;
    CursorAdapter mCsrAdapter;
    Cursor mCsr;
    ImageView mImageView;
    DBHelper mDBHlpr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // get the View/Viewgroup IDs
        mListView01 = this.findViewById(R.id.listview001); // File List
        mListView02 = this.findViewById(R.id.listview002); // DB List
        mImageView = this.findViewById(R.id.imageview001); // Image display

        // get an instance of the DBHelper
        mDBHlpr = new DBHelper(this);

        // copy images from raw folder to data/data/<package>/Files/images
        // Also store all the images in the Database (or not depedning upon size)
        getimagesFile(this);
        if (getimagesCount() < 1) {
            loadRawImages();
            storeImagesToDB();
        }
        // Setup the two ListViews to display image name lists
        displayList();
        displayListFromDB();

        // setup the file list so that when an item is clicked the image is displayed
        mListView01.setonItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String imagename = mListView01.getItemAtPosition(i).toString();
                displayImage(imagename);
            }
        });

        // setup the DB list so that when an item is clicked the image is displayed
        mListView02.setonItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                displayDBImage(l);
            }
        });
    }

    /**
     * Store the images in the images folder to the DB giving then a calculated description
     * e.g. image1, image2 .....
     */
    private void storeImagesToDB() {
        File f = getimagesFile(this);
        File[] images = f.listFiles();
        int imagecounter = 1;
        for (File img: images) {
            mDBHlpr.addImageFromPath(img.getPath(),"image" + String.valueOf(imagecounter++));
        }
    }

    /**
     * return the directory/folder where the images are stored as a File
     * @param context   a valid context
     * @return          the number of images
     */
    public static File getimagesFile(Context context) {
        if (images_file == null) {
            images_file = new File(context.getFilesDir().getPath() + File.separator + IMAGES_DIRECTORY);
            if (!images_file.exists()) {
                images_file.mkdirs();
            }
        }
        return images_file;
    }

    /**
     * get the number of images
     * @return the number of images
     */
    public static long getimagesCount() {
        File[] files = images_file.listFiles(new FileFilter() {
            @Override
            public boolean accept(File file) {
                return file.isFile();
            }
        });
        return (long) files.length;
    }

    /**
     * Setup/refresh the list of images according to the images folder
     * (left ListView)
     */
    private void displayList() {
        if (mImages == null) {
            mImages = new ArrayList<>();
        } else {
            mImages.clear();
        }
        mImages.addAll(Arrays.asList(images_file.list()));
        if (mAdapter == null) {
            mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, mImages);
            mListView01.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
    }

    /**
     * Set the image view according to the file
     * @param imageName the name of the image (as per the ListView)
     */
    private void displayImage(String imageName) {
        File img = new File(images_file.getPath() + File.separator + imageName);
        if (img.exists()) {
            Bitmap bmp = BitmapFactory.decodeFile(img.getAbsolutePath());
            mImageView.setimageBitmap(bmp);
        }
    }

    /**
     * Set the image view according to the image stored/referred to by the DB
     * @param id    the id of the respective row in the image table
     */
    private void displayDBImage(long id) {
        mImageView.setimageBitmap(mDBHlpr.getimage(id));
    }

    /**
     * Setup/refresh the list of images as obtained from the DB (right listview)
     */
    private void displayListFromDB() {
        mCsr = mDBHlpr.getimageList();
        if (mCsrAdapter == null) {
            mCsrAdapter = new SimpleCursorAdapter(
                    this,
                    android.R.layout.simple_list_item_2,
                    mCsr,
                    new String[]{DBHelper.COL_IMAGE_NAME,DBHelper.COL_IMAGE_PATH},
                    new int[]{android.R.id.text1,android.R.id.text2},
                    0
            );
            mListView02.setAdapter(mCsrAdapter);
        } else {
            mCsrAdapter.swapCursor(mCsr);
        }
    }

    /**
     * Load (copy from raw folder to images folder) all images
     */
    private void loadRawImages() {
        Field[] fields = R.raw.class.getFields();
        int resourceID = 0;
        String resourceName;
        for (Field fld: fields) {
            resourceName = fld.getName();
            try {
                resourceID = fld.getInt(fld);
            } catch (illegalaccessexception e) {
                e.printstacktrace();
            }
            Log.d("RAW FLDINFO","name=" + fld.getName() + " ID=" + String.valueOf(resourceID));
            copyResourceImagetoImages(resourceID,resourceName, true);
        }
    }

    /**
     * copy an image from the raw directory (app/src/main/res/raw directory) to
     *  the Apps data/data/files/images folder
     * @param resourceID        ID of the resource
     * @param resourceName      name of the resource (file name less extension)
     * @param throw_exception   true if an exception should be thrown
     */
    private void copyResourceImagetoImages(int resourceID, String resourceName, boolean throw_exception) {
        String tag = "cpyRSRCTOIMAGES";
        InputStream is = getResources().openRawResource(resourceID);
        File of = new File(images_file.getPath() + File.separator + resourceName + ".jpg");
        Log.d(tag,"Initiating copy of File " + of.getName());
        int buffer_size = 1024 * 4;
        int bytesread = 0;
        long bytescopied = 0;
        OutputStream os;
        byte[] buffer = new byte[buffer_size];
        if (!of.exists()) {
            try {
                of.createNewFile();
            } catch (IOException e) {
                Log.d(tag,"Error Creating File " + of.getName());
                e.printstacktrace();
                if (throw_exception) {
                    throw new RuntimeException("Error Creating Output File" + of.getName());
                }
                return;
            }
        }
        try {
            os = new FileOutputStream(of);
        } catch (IOException e) {
            Log.d(tag,"Error Creating OutputStream for File " + of.getName());
            e.printstacktrace();
            if (throw_exception) {
                throw new RuntimeException("Error Creating OutputStream for File " + of.getName());
            }
            return;
        }
        if (os == null) {
            throw new RuntimeException("OutputStream not initialised.");
        }
        try {
            while ((bytesread = is.read(buffer)) > 0 ){
                try {
                    os.write(buffer, 0, bytesread);
                } catch (IOException e) {
                    String msg = "Error Writing to Output File " + of.getName() + " Bytes copied = " + bytescopied;
                    Log.d(tag, msg);
                    e.printstacktrace();
                    if (throw_exception) {
                        os.close();
                        of.delete();
                        throw new RuntimeException(msg);
                    }
                    is.close();
                    os.close();
                    of.delete();
                    return;
                }
                bytescopied = bytescopied + bytesread;
            }
        }catch (IOException e) {
            String msg = "Error reading Input File " + resourceName + " Bytes copied = " + bytescopied;
            Log.d(tag,"Error Reading Input File " + resourceName);
            e.printstacktrace();
            if (throw_exception) {
                throw new RuntimeException(msg);
            }
            try {
                is.close();
                os.close();
                of.delete();
            } catch (IOException e2) {
                e2.printstacktrace();
            }
        }
        Log.d(tag,"File " + of.getName() +" copied - Bytes Successfully copied = " + bytescopied);
        try {
            os.flush();

        } catch (IOException e) {
            e.printstacktrace();
        }

        try {
            os.close();
        } catch (IOException e) {
            e.printstacktrace();
        }
        try {
            is.close();
        } catch (IOException e) {
            e.printstacktrace();
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐